TableViewCellWithAutoLayoutiOS8 icon indicating copy to clipboard operation
TableViewCellWithAutoLayoutiOS8 copied to clipboard

Adding new cell while scrolling causes table view to jump

Open rainypixels opened this issue 10 years ago • 6 comments

If you...

  1. Swipe the tableview to scroll it fast (or slow).
  2. 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.

rainypixels avatar Jul 31 '15 06:07 rainypixels

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.

rainypixels avatar Jul 31 '15 19:07 rainypixels

This stops the tableView from scrolling and therefore prevents the glitch: tableView.setContentOffset(tableView.contentOffset, animated: false)

Hope it helps someone

budidino avatar Nov 02 '16 19:11 budidino

@budidino when do you call it?

yarodevuci avatar Nov 07 '18 00:11 yarodevuci

@budidino when do you call it?

call "tableView.setContentOffset(tableView.contentOffset, animated: false)" just before you reloadData()

tickendy avatar Nov 19 '18 21:11 tickendy

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.

dashared avatar Jan 29 '19 19:01 dashared

This actually helped me (see https://forums.developer.apple.com/thread/86703)

    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;

appleios avatar Dec 11 '19 16:12 appleios