scrollToRowAtIndexPath 滚动到倒数第二行而不是最后一行
我有一个 messagesTableView,它显示在 NavigationController 的 ViewController 中。
在 viewDidLoad 中:
[self.messagesTableView reloadData];
NSInteger t;
if ((t = [self.messagesTableView numberOfRowsInSection:0]) > 0) {
NSLog(@"ViewDidLoad: number of rows:%d Messages Count:%d", t, [messages count]);
NSIndexPath* ip = [NSIndexPath indexPathForRow: (t-1) inSection:0];
[self.messagesTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionNone animated:NO];
}
在 viewDidAppear 中:
[self.messagesTableView reloadData];
NSInteger t;
if ((t = [self.messagesTableView numberOfRowsInSection:0]) > 0) {
NSLog(@"ViewDidAppear: number of rows:%d Messages Count:%d", t, [messages count]);
NSIndexPath* ip = [NSIndexPath indexPathForRow: (t - 1) inSection:0];
[self.messagesTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
viewDidLoad 中的scrollToRowAtIndexPath 滚动到倒数第二行而不是最后一行,而在viewDidAppear 滚动到最后一行。唯一的问题是调用 viewDidAppear 函数需要一段时间。
但是为什么它在 viewDidLoad 和 viewDidAppear 中的行为不同(NSLog 为两者打印相同的值)?
I have a messagesTableView which is shown in a ViewController in a NavigationController.
In viewDidLoad:
[self.messagesTableView reloadData];
NSInteger t;
if ((t = [self.messagesTableView numberOfRowsInSection:0]) > 0) {
NSLog(@"ViewDidLoad: number of rows:%d Messages Count:%d", t, [messages count]);
NSIndexPath* ip = [NSIndexPath indexPathForRow: (t-1) inSection:0];
[self.messagesTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionNone animated:NO];
}
and in viewDidAppear:
[self.messagesTableView reloadData];
NSInteger t;
if ((t = [self.messagesTableView numberOfRowsInSection:0]) > 0) {
NSLog(@"ViewDidAppear: number of rows:%d Messages Count:%d", t, [messages count]);
NSIndexPath* ip = [NSIndexPath indexPathForRow: (t - 1) inSection:0];
[self.messagesTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
The scrollToRowAtIndexPath in viewDidLoad scrolls to second last row instead of the last row whereas the same function in viewDidAppear scrolls to the last row.. The only problem is that it takes a while for the viewDidAppear function to be called..
but why does it behave differently in viewDidLoad and viewDidAppear (the NSLog is printing the same values for both)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
viewDidAppear 在滑入动画完成后调用。您可能想尝试在 viewWillAppear 中设置表视图位置,以便表视图在可见时立即显示在正确的位置。
我认为 viewDidLoad 在 loadView 之后、表视图的 layoutSubviews 之前被调用。在此阶段,表格视图添加了坐标,不考虑导航栏。然后,当 viewController 被推送时,navigationController 会调用另一个 setFrame 来将 tableView 控制器的视图放入其区域。这会将表格视图向下移动大约 44 点,导致底部单元格消失。
所有这些大小调整都在 viewWillAppear 中完成,因此这是执行此操作的最佳位置。
viewDidAppear is called after ther slide-in animation is complete. You might want to try to set the table view position in viewWillAppear instead, so that the tableview shows at the correct position immediately when it becomes visible.
I think that viewDidLoad gets called after loadView, but before layoutSubviews of the table view. The tableview is added with coordinates not considering the navigation bar at this stage. Then when the viewController is pushed there is another setFrame called by the navigationController to fit the tableView controller's view into it's area. This moves the table view down by approx 44 points, causing the bottom cell to disappear.
All this resizing is over in viewWillAppear, so that's the best place to do that.
只需将您的
scrollToBottom
代码:放入
-(void)viewWillLayoutSubviews
方法中;并注意确保
animated:NO
;Just place your
scrollToBottom
code:into
-(void)viewWillLayoutSubviews
method;And note that make sure
animated:NO
;