如何在iOS 7中将UITableView滚动到顶部

5
在iOS 7中,我使用以下代码来滚动到我的UITableView顶部。您需要考虑半透明状态栏和导航栏的重叠。请参见UITableView
[tableView 
    setContentOffset:CGPointMake(
        0.0, 
        -tableViewController.topLayoutGuide.length
    ) 
    animated:YES
];

这只在第一次调用后才有效。在第一次调用时,我的表格滚动的距离比应该滚动的多得多,显示了很多空白区域。此外,UIRefreshControl 看起来会卡住。你需要轻微地触碰表格,使它反弹到真正的顶部。之后,无论你调用多少次,它都会像你预期的那样运行。

enter image description here enter image description here

我尝试过其他方法,但它们都存在问题。iOS 6 的方法在第一次调用时也会表现得非常奇怪。虽然在后续调用中它不会突然跳跃很多,但由于我们忘记考虑状态栏和导航栏,所以它们并不正确,因为它会将表格滚动到距离顶部 64.0 点的位置。

[table setContentOffset:CGPointZero animated:YES];

我还尝试了滚动到第一个单元格,但它不能一次性滚动到顶部。每次调用它时,它只会向上滚动一页的内容。

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

尝试这个答案 https://dev59.com/eHRB5IYBdhLWcg3wFz8g?rq=1 - Duncan Groenewald
1
@DuncanGroenewald [tableView scrollRectToVisible:CGRectMake(0.0, 0.0, 1.0, 1.0) animated:YES] 只能每次滚动一屏。我必须多次调用它才能滚动到第一个单元格。也许这只是我的代码问题...我不知道... - Pwner
8个回答

17

试试这个:

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

在 SWIFT 中

    let top = NSIndexPath(forRow: NSNotFound , inSection: 0)
    tableView.scrollToRowAtIndexPath(top, atScrollPosition: .Bottom, animated: true)

Swift 4.0及以上版本

 let top = NSIndexPath(row: NSNotFound, section: 0)
 tableView.scrollToRow(at: top as IndexPath, at: .bottom, animated: true)

就像其他尝试一样,这只能向上滚动一个屏幕的内容。如果我已经向下滚动了多个屏幕,我必须多次调用它。 - Pwner

6

我查看了其他答案并找到了以下对我有效的解决方案。

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

希望这能解决你的问题。


2

这并不是“魔法”。64的偏移量是为了状态栏(20点)和导航栏(44点)的高度。如果滚动视图的偏移量为0,并且您还有状态栏+导航栏,则它将位于这些对象下方,您将看到原始内容的-64.0。在Storyboard中有一个选项“调整滚动视图插图”,默认情况下已经勾选。


0

在Objective-C中

[mainTableView setContentOffset:CGPointZero animated:YES];

在 Swift 中:

mainTableView.setContentOffset(CGPointZero, animated:true)

0

在 Objective C 中

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

在Swift中

var scrollIndexPath: NSIndexPath = NSIndexPath(forRow:NSNotFound , inSection: 0)
self.tableview.scrollToRowAtIndexPath(scrollIndexPath, atScrollPosition: UITableViewScrollPosition.top, animated: true)

0

尝试:

[tableView
    setContentOffset:CGPointMake(
        tableView.contentOffset.x,
        -tableView.contentInset.top
    )
    animated:YES
];

这只会滚动一个屏幕高度的单元格,而不是整个表格的高度。 - Pwner
听起来对我来说像是异常行为。根据苹果的文档,设置contentOffset应该直接带您到指定的偏移量。您使用的是普通的UITableView吗? - skedastik

0
请尝试这个:
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

示例就像这样:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:0 animated:YES];

-1
iOS的滚动视图表现有点奇怪。正如你所提到的,64.0偏移量“神奇地”添加到了视图层次结构中的第一个滚动视图上,“第一次”出现这种情况时我还没有找出原因。目前我只有一个非常hackish的解决方案:您可以将一个虚拟滚动视图添加为视图层次结构中的第一个滚动视图,并将其高度设置为0。之后,您的解决方案应该像往常一样工作。截图:http://imgur.com/LvrbcqG希望这能帮到你。
还有其他人知道这是为什么吗?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接