如何在IOS7 UITableView中滚动到顶部?

时间:2013-10-08 08:52:47

标签: ios uitableview ios7

在IOS6中,我有以下代码滚动到UITableView的顶部

[tableView setContentOffset:CGPointZero animated:YES];

在IOS7中,这不再起作用了。表格视图不会完全滚动到顶部(但差不多)。

12 个答案:

答案 0 :(得分:127)

在iOS7中,默认情况下,整个屏幕的UITableView和UIScrollView组件会调整内容并滚动指示符插入,以使一切正常。但是,正如您所注意到的那样,CGPointZero不再代表将您带到视觉“顶部”的内容偏移。

请改用:

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

在这里,您不必担心是否有部分或行。您也不会告诉Table View定位第一行,然后想知道为什么它没有显示所有非常高的表头视图等。

答案 1 :(得分:23)

试试这个:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

答案 2 :(得分:22)

基于@Markus Johansson接受的答案,这里是Swift代码版本:

func scrollToTop() {
    if (self.tableView.numberOfSections > 0 ) {
        let top = NSIndexPath(row: Foundation.NSNotFound, section: 0)
        self.tableView.scrollToRow(at: top as IndexPath, at: .top, animated: true);
    }
}

答案 3 :(得分:17)

在其他答案的帮助下,我设法让它运转起来。为了避免崩溃,我必须首先检查是否有一些部分。如果第一部分没有行,则NsNotFound可用作行索引。希望这应该是放在UITableViewController中的通用函数:

-(void) scrollToTop
{
    if ([self numberOfSectionsInTableView:self.tableView] > 0)
    {
        NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
        [self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}

答案 4 :(得分:4)

var indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.sampleTableView.scrollToRowAtIndexPath(indexPath,
atScrollPosition: UITableViewScrollPosition.Top, animated: true)

self.sampleTableView.setContentOffset(CGPoint.zero, animated:false)

答案 5 :(得分:3)

float systemVersion= [[[UIDevice currentDevice] systemVersion] floatValue];

if(systemVersion >= 7.0f)
{
  self.edgesForExtendedLayout=UIRectEdgeNone;   
}

viewDidLoad()方法中试用此代码。

答案 6 :(得分:3)

这是更新的Swift 3语法中的idStar的answer

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

动画:

self.tableView.setContentOffset(CGPoint(x: 0, y: 0 - self.tableView.contentInset.top), animated: true)

答案 7 :(得分:3)

Swift 3

如果您有表格视图标题CGPointZero可能不适合您,但这总是可以滚动到顶部。

self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: false)

答案 8 :(得分:2)

您仍然可以将scrollToRowAtIndexPath:用于此目的

答案 9 :(得分:2)

我意识到这已得到回答,但我只想提出另一种选择:

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

这始终保证UITableView将滚动到顶部。接受的答案是:

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

对我不起作用,因为我正在滚动到tableHeaderView而不是单元格。

使用scrollRectToVisible适用于iOS 6和7。

答案 10 :(得分:1)

Swift 4 UITableViewExtension:

func scrollToTop(animated: Bool) {
    if numberOfSections > 0 {
        let topIndexPath = IndexPath(row: NSNotFound, section: 0)
        scrollToRow(at: topIndexPath, at: .top, animated: animated)
    }
}

答案 11 :(得分:0)

我在swift中使用过:

self.tableView.setContentOffset(CGPointMake(0, 0), animated: true)

但@Alvin George的作品很棒