How to get a UITableview to go to the top of page on reload?

AppsToKnow picture AppsToKnow · Apr 4, 2011 · Viewed 27.9k times · Source

I am trying to get a UITableview to go to the top of the page when I reload the table data when I call the following from

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    // A bunch of code...

    [TableView reloadData];
}

Along those same lines, I would also like to be able to go to a specific section when I reload the table data.

I tried placing the following, which seems applicable only to the row, before and after the reload, but nothing happens:

[TableView scrollToRowAtIndexPath:0 atScrollPosition:UITableViewScrollPositionTop  animated:YES];

Answer

Erik B picture Erik B · Apr 4, 2011

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.