UITableView scrollToRowAtIndexPath 的奇怪行为:
我必须将 UITableView 的初始滚动位置设置为第一行以外的行(例如,第二部分的第一行)。
在我的视图控制器的 viewDidLoad 中,我写道:
- (void)viewDidLoad
{
// self.view is a container view, not UITableView
[super viewDidLoad];
[table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]
atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
当屏幕加载时,滚动位置不会立即设置。否则,当 animated 参数设置为 YES 时,将立即设置位置,而不使用任何动画。 发生了什么事?
设置 UITableView 初始行位置的正确方法是什么?
I have to set an initial scroll position of my UITableView to a row other than the first one (for example, the first row of the second section).
In my view controller's viewDidLoad I write:
- (void)viewDidLoad
{
// self.view is a container view, not UITableView
[super viewDidLoad];
[table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]
atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
When the screen loads, the scroll position is not set immediately. Otherwise, when the animated parameter is set to YES, the position is set immediately without any animation. What's going on?
What's the correct way to set initial row position for UITableView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好的地方应该是ViewDidAppear。此外,您应该调用 cellWillDisplay 中的滚动方法来确定。请记住在调用滚动方法之前检查indexPath
Best place should be ViewDidAppear. Moreover, you should invoke scroll method in cellWillDisplay to make sure. Remember to check indexPath before invoking scroll method
只是瞎猜,但是您是否尝试过将此代码放入 viewDidAppear{} 中?
Just a shot in the dark, but have you tried to put this code into viewDidAppear{}?
这在
viewDidLoad
或viewWillAppear
中不起作用,因为那时表很可能尚未填充(至少在我的情况下没有)尝试将其放入一旦你的表加载并调用它,就会使用 NSTimer 来代替。
顺便说一句,我很快将此代码(带有动画)放入导航栏按钮项目中,并且在表格加载完成后效果非常好。
This won't work in
viewDidLoad
orviewWillAppear
since the table most likely haven't been populated by then (at least it wasn't in my case)Try putting it in an NSTimer instead once your table loads and calling it then.
As an aside, I quickly put this code (with animation) into a nav bar button item and it worked very well after the table was finished loading.