When did set change heightForRowAt UITableView animation is broken

Viewed 126

When did set change heightForRowAt UITableView animation is broken cell is a jumped. If selected last row and scroll on top and after a tap for collapsed row the table jumps to up. The animation is broke.

enter image description here

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()
}
1 Answers

When I was beginning using this method, the animation looks very good.

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