iOS 8:UITableView滚动到行索引不起作用

时间:2015-02-16 19:56:40

标签: uitableview ios8 header searchbar

在我的表重新加载后在viewDidLoad中使用这行代码。

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

不幸的是,它不适合我。 抛出异常:

Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (0) beyond bounds (0) for section (0).

我想在应用程序启动时隐藏tableView的标题部分。

标题部分包含searchBar。所以我希望它在应用程序启动时隐藏。 我搜索得很辛苦,但无法找到问题的确切答案。作为最后的手段,我不得不自己打字。

此致

天真的iOS开发者。

1 个答案:

答案 0 :(得分:1)

当某个部分没有行时会发生这种情况。在这种情况下使用NSNotFound。您可以在UITableView类参考中找到它。

<强>的OBJ-C

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

<强>夫特

let scrollIndexPath = NSIndexPath(forRow: NSNotFound, inSection: 0)
tableView.scrollToRowAtIndexPath(scrollIndexPath, atScrollPosition: .Top, animated: true)
相关问题