18

我想完全重置 UITableView 的滚动位置,以便每次打开它时,它都会显示最顶部的项目。换句话说,我想每次打开表格视图时都将其滚动到顶部。

我尝试使用以下代码,但看起来我误解了文档:

- (void)viewWillAppear:(BOOL)animated {
    [tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:NO];
}

这是错误的方法吗?

4

4 回答 4

34

August 得到了 UITableView 特有的方法。另一种方法是:

[tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

该方法在 UITableView 的父类 UIScrollView 中定义。上面的示例告诉它滚动到 0,0 处的 1x1 框 - 换句话说,左上角。

于 2008-11-21T05:25:34.140 回答
6

您正在使用的方法滚动到(正如方法名称所暗示的)最近的选定行。在许多情况下,这不会是第一行。相反,您想使用

scrollToRowAtIndexPath:atScrollPosition:animated:

或者

selectRowAtIndexPath:animated:scrollPosition:

您在其中使用要滚动到的行的索引路径。第二种方法实际上选择了一行,第一种方法只是滚动到它。

于 2008-11-21T05:06:47.220 回答
0

我最终做的是:

斯威夫特 4

self.tableView.contentOffset = CGPoint.zero
DispatchQueue.main.async {
    self.tableView.reloadData()
}
于 2018-05-03T17:56:51.037 回答
0

迅速:

self.tableView.scrollRectToVisible(CGRect.zero, animated: false)
于 2021-03-30T12:30:46.083 回答