I want to update the bottom cell in my UITableView, and scroll the view down all the way so that the entirety of the new cell is visible. (The cell's old content is about 50 pixels tall; the new content is about 100 pixels tall. And the table's content is much taller than its bounding view.) But when I do this:
tableView.beginUpdates()
tableView.reloadRows(at: [myIndexPath], with: .none)
tableView.endUpdates()
tableView.setContentOffset(
CGPoint(x: 0, y: tableView.contentSize.height - tableView.bounds.size.height),
animated: true)
then, instead, I get scrolled upwards to above the top of my tableView. The problem is that, the moment endUpdates
is called, tableView.contentSize.height
suddenly shrinks by a few hundred pixels, so that it's smaller than the bounding view. I don't understand why that is.
I've found that if I add a delay of about 500ms or longer around setContentOffset
, then things work properly, but I don't want to have to use a delay.
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
self.tableView.setContentOffset(CGPoint(x: 0, y: self.tableView.contentSize.height - self.tableView.bounds.size.height),
animated: true)
}
I've tried using a CATransation, per https://stackoverflow.com/a/35228192/548862, but that's not having any effect on the problem.
I have a tableView estimatedHeightForRowAt
method to estimate the row heights (from a set of constants), but I don't see how that could be messing me up.
What am I doing wrong?