Markus Johansson Markus Johansson - 1 year ago 81
iOS Question

How to scroll to top in IOS7 UITableView?

In IOS6 I have the following code to scroll to the top of a UITableView

[tableView setContentOffset:CGPointZero animated:YES];

In IOS7 this doesn't work anymore. The table view isn't scrolled completely to the top (but almost).

Answer Source

By the help from some other answers here I managed to get it working. To avoid a crash I must first check that there are some sections. NsNotFound can be used as a row index if the first section has no rows. Hopefully this should be a generic function to be placed in a UITableViewController:

-(void) scrollToTop
    if ([self numberOfSectionsInTableView:self.tableView] > 0)
        NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
        [self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download