How to scroll to top in IOS7 UITableView?

后端 未结 12 1807
粉色の甜心
粉色の甜心 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:28

    I realize this has been answered but I just wanted to give another option:

    CGRect frame = {{0, 0},{1, 1}};
    [self.tableView scrollRectToVisible:frame animated:YES];
    

    This always guarantees the UITableView will scroll to the top. The accepted answer:

    NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
    [self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];
    

    was not working for me because I was scrolling to a tableHeaderView and not a cell.

    Using scrollRectToVisible works on iOS 6 and 7.

提交回复
热议问题