How to scroll to top in IOS7 UITableView?

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

    Here is idStar's answer in updated Swift 3 syntax:

    self.tableView.contentOffset = CGPoint(x: 0, y: 0 - self.tableView.contentInset.top)
    

    With animation:

    self.tableView.setContentOffset(CGPoint(x: 0, y: 0 - self.tableView.contentInset.top), animated: true)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-13 00:33

    In iOS7, whole screen UITableView and UIScrollView components, by default, adjust content and scroll indicator insets to just make everything work. However, as you've noticed CGPointZero no longer represents the content offset that takes you to the visual "top".

    Use this instead:

    self.tableView.contentOffset = CGPointMake(0, 0 - self.tableView.contentInset.top);
    

    Here, you don't have to worry about if you have sections or rows. You also don't tell the Table View to target the first row, and then wonder why it didn't show all of your very tall table header view, etc.

    0 讨论(0)
  • 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];
        }
    }
    
    0 讨论(0)
  • 2020-12-13 00:35
    var indexPath = NSIndexPath(forRow: 0, inSection: 0)
    self.sampleTableView.scrollToRowAtIndexPath(indexPath,
    atScrollPosition: UITableViewScrollPosition.Top, animated: true)
    

    or

    self.sampleTableView.setContentOffset(CGPoint.zero, animated:false)
    
    0 讨论(0)
  • 2020-12-13 00:38

    Swift 4 UITableViewExtension:

    func scrollToTop(animated: Bool) {
        if numberOfSections > 0 {
            let topIndexPath = IndexPath(row: NSNotFound, section: 0)
            scrollToRow(at: topIndexPath, at: .top, animated: animated)
        }
    }
    
    0 讨论(0)
提交回复
热议问题