Adding new cell while scrolling causes table view to jump
If you...
- Swipe the tableview to scroll it fast (or slow).
- Hit the '+' button while it's scrolling
... the tableview jumps up before it continues scrolling from a random location. This doesn't happen if the tableview is not scrolling (i.e. it's stationary) when you add cells. I'm assuming this is another UITableView auto-sizing bug that has something to do with scrollView frame and offset calculations when the table view is being scrolled.
Anyone have any ideas for a workaround? I wanted to check before I try to get too clever.
Turns out that if you change the addRow method to use reloadData as follows, the jumping issue disappears.
func addRow()
{
model.addSingleItem()
self.tableView.reloadData()
}
It certainly seems like insertRowsAtIndexPath:withRowAnimation: is the problem here because it probably attempts to adjust scrollview height immediately but by using the estimatedRowHeight of the newly inserted rows.
Calling reloadData whenever a couple of cells are added certainly seems like a less than ideal workaround, so I'm still interested to see if anyone has better ideas.
This stops the tableView from scrolling and therefore prevents the glitch:
tableView.setContentOffset(tableView.contentOffset, animated: false)
Hope it helps someone
@budidino when do you call it?
@budidino when do you call it?
call "tableView.setContentOffset(tableView.contentOffset, animated: false)" just before you reloadData()
https://brigade.engineering/reloading-uitableview-while-animating-scroll-in-ios-11-f89910c958b6 hope this will help somebody! I personally found it very useful while I stuck with the problem how to delete rows while scrolling.
This actually helped me (see https://forums.developer.apple.com/thread/86703)
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;