How to scroll to top in IOS7 UITableView?

后端 未结 12 1825
粉色の甜心
粉色の甜心 2020-12-13 00:25

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

[tableView setContentOffset:CGPointZero animated:YES];

In IOS7 this

12条回答
  •  悲哀的现实
    2020-12-13 00:33

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

提交回复
热议问题