untitled
untitled

Reputation: 405

UITableView scrolls to top when inserting more rows

In my code, I've overridden tableView:willDisplayCell, and when the last row is shown, I start a load for more data. When this data comes in, I call insertRowsAtIndexPaths to add the rows.

This generally works (the tableview's scroll position doesn't change), but I've noticed that if I overscroll and not let go (scroll past the end of the tableview) that the tableview will jump to the top when insertRowsAtIndexPaths is called upon data load.

I'm using UITableViewAutomaticDimension/Autolayout for my cell heights, as each cell has different heights depending on the data they contain. How can I avoid this issue? Ideally if the data loads and the tableview is over scrolled, the scroll position would stay at the overscroll once the data comes in.

Upvotes: 1

Views: 1231

Answers (1)

MikeG
MikeG

Reputation: 4044

You can create a reference on the bottom cell indexpath, and in whichever function is causing it to go back to the top, call

[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];

SWIFT 3

    self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.none, animated: true)

Upvotes: 1

Related Questions