xcode UITableView 不滚动到单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2910503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView does not scroll to cell
提问by Sheehan Alam
I am trying to scroll my tableview to the 2nd cell:
我正在尝试将我的 tableview 滚动到第二个单元格:
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]
atScrollPosition:UITableViewScrollPositionNone
animated:NO];
I get the error:
我收到错误:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: section (1) beyond bounds (0).
'
'
My tableview has 30 cells that are appearing with no sections.
我的 tableview 有 30 个单元格,没有任何部分。
采纳答案by Laurent Etiemble
If you have no sections, then you can try the indexPathWithIndex:
class constructor:
如果您没有节,那么您可以尝试indexPathWithIndex:
类构造函数:
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndex:1]
atScrollPosition:UITableViewScrollPositionNone
animated:NO];
回答by pollyp
When I got the same error message with a one-section table, I fixed it by creating a NSIndexPath with both a row and section:
当我在单节表中收到相同的错误消息时,我通过创建一个包含行和节的 NSIndexPath 来修复它:
NSIndexPath *ip = [NSIndexPath indexPathForRow:itemCurrentlyPlaying inSection:0];
It's not easy to find, but the relevant convenience constructor is documented here:
不容易找到,但相关的便利构造函数记录在此处:
回答by Jay Goodman Tamboli
Like pollyp, I was able to use indexPathForRow:inSection:
. I'm calling that from my viewWillAppear:
.
像 pollyp 一样,我能够使用indexPathForRow:inSection:
. 我从我的viewWillAppear:
.
It looks like your error message is saying that there isn't even one section. Are you perhaps calling scrollToRowAtIndexPath:atScrollPosition:animated:
before the table data is loaded (e.g. in viewDidLoad
)? Try calling it later, in viewWillAppear:
.
看起来您的错误消息是说甚至没有一节。您是否可能scrollToRowAtIndexPath:atScrollPosition:animated:
在加载表数据之前调用(例如 in viewDidLoad
)?稍后尝试在viewWillAppear:
.
回答by fbartho
I recommend you create a section to store your cells in. This section can have no header so that it's not visually distinguishable, and what this gains you is the ability to not have to fight against the intended API pathways.
我建议您创建一个部分来存储您的单元格。该部分可以没有标题,因此它在视觉上无法区分,这使您能够不必与预期的 API 路径作斗争。