【问题标题】:Cells temporarily disappearing when TableView beginUpdates / endUpdatesTableView beginUpdates / endUpdates 时单元格暂时消失
【发布时间】:2017-03-08 07:57:02
【问题描述】:

我正在尝试制作一个可扩展的表格视图(静态单元格)。容器视图放置在“选项列表”单元格下方的单元格内,如下所示:

问题在于展开/折叠时的动画。每个部分的最上面的单元格会短暂消失(隐藏?),然后在动画结束后重新出现。后来我做了实验,结果如下:

使用 tableView.reloadData() 时不会发生这种情况。

这会在使用 tableView.beginUpdates() / endUpdates() 对时发生。

因此我得出结论,这与动画有关。下面是我的代码和图片,用于进一步解释上述问题。

隐藏/显示代码

private func hideContainerView(targetView: UIView) {
    if targetView == violationOptionsContainerView {
        violationOptionContainerViewVisible = false
    }
    tableView.beginUpdates()
    tableView.endUpdates() 

    UIView.animate(withDuration: 0.1, animations: { targetView.alpha = 0.0 }) { _ in
        targetView.isHidden = true
   }
}

private func showContainerView(targetView: UIView) {
    if targetView == violationOptionsContainerView {
        violationOptionContainerViewVisible = true
    }
    tableView.beginUpdates()
    tableView.endUpdates()
    targetView.alpha = 0.0
    UIView.animate(withDuration: 0.1, animations: { targetView.alpha = 1.0 }) { _ in
        targetView.isHidden = false
    }
}

表格视图

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) {
        switch cell {
        case violationTableViewCell:
            if violationOptionContainerViewVisible {
                hideContainerView(targetView: violationOptionsContainerView)
            } else {
                showContainerView(targetView: violationOptionsContainerView)
            }
        default: break
        }
        // tableView.reloadData()
        // if we use this instead of begin/end updates, the cells won't disappear. But then we'll be ditching animation too.     
    }
    tableView.deselectRow(at: indexPath, animated: true)
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == 0 && indexPath.row == 3 {
        if !violationOptionContainerViewVisible {
            return 0.0
        }
    }
    return super.tableView(tableView, heightForRowAt: indexPath)
}

图片

请注意,每个部分中的第一个单元格(“FFFF”和“滑块”)在动画期间消失并重新出现

【问题讨论】:

    标签: ios uitableview animation


    【解决方案1】:

    经过几天的搜索,我找到了这种行为的原因。

    出于某种原因,我将单元格layer.zposition 设置为低于默认值零。这将导致单元格在动画期间被看到“低于”它的背景视图(因此消失),即使它是它的子视图。

    将值调整回 0 或更高将消除此问题。

    【讨论】:

      猜你喜欢
      • 2011-12-31
      • 2016-12-01
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多