【发布时间】: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