【问题标题】:When did set change heightForRowAt UITableView animation is broken什么时候设置更改 heightForRowAt UITableView 动画被破坏
【发布时间】:2024-05-23 00:40:02
【问题描述】:

什么时候设置改变 heightForRowAt UITableView 动画是破碎的单元格是跳跃的。如果选择最后一行并在顶部滚动,点击折叠行后,表格会向上跳。动画坏了。

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
    tableView.estimatedRowHeight = 300
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return conditionData[indexPath.section].conditions?[indexPath.row].selected ?? false ? 300 : 76
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    conditionData[indexPath.section].conditions?[indexPath.row].selected = true
    tableView.beginUpdates()
    let cell = tableView.cellForRow(at: indexPath) as? ConditionsCell
    cell?.setSelected(true, animated: true)
    tableView.endUpdates()
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    conditionData[indexPath.section].conditions?[indexPath.row].selected = false
    tableView.beginUpdates()
    let cell = tableView.cellForRow(at: indexPath) as? ConditionsCell
    cell?.setSelected(false, animated: true)
    tableView.endUpdates()
}

【问题讨论】:

  • 在展开另一个单元格时,您是否正在折叠展开的单元格?
  • 如果在 tableView.beginUpdates() 之前添加 UIView.setAnimationsEnabled(false) 并设置 true 如何 endUpdates。调整单元格行高时我丢失了所有动画。
  • tableView 是否将allowsMultipleSelection 设置为true?
  • 是的,我设置了allowsMultipleSelection = true

标签: ios swift uitableview uiviewanimation


【解决方案1】:

当我开始使用这种方法时,动画看起来非常好。

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return conditionData[indexPath.section].conditions?[indexPath.row].selected ?? false ? 300 : 76
}

【讨论】: