什么时候设置改变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()
}
当我开始使用此方法时,动画看起来非常好。
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return conditionData[indexPath.section].conditions?[indexPath.row].selected ?? false ? 300 : 76
}