How to get a UITableview to go to the top of page on reload?
Here's another way you could do it:
Objective-C
[tableView setContentOffset:CGPointZero animated:YES];
Swift 3 and higher
tableView.setContentOffset(.zero, animated: true)
Probably the easiest and most straight forward way to do it.
To scroll to a specific section you need to specify the index path to it:
NSIndexPath *topPath = [NSIndexPath indexPathForRow:0 inSection:0];
[TableView scrollToRowAtIndexPath:topPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
If you are uncertain about the number of rows in your tableView. Use the following:
NSIndexPath *topPath = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
[TableView scrollToRowAtIndexPath:topPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
After reading all of the answers here and elsewhere, I finally found a solution that works reliably and does not show the scrolling after the data is loaded. My answer is based on this post and my observation that you need a negative offset if you are using content insets:
func reloadAndScrollToTop() {
tableView.reloadData()
tableView.layoutIfNeeded()
tableView.contentOffset = CGPoint(x: 0, y: -tableView.contentInset.top)
}