Search code examples
iphonecocoa-touchtableview

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


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];

Solution

  • 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.