[英]Scroll to bottom in UITableView
I have a chatScreen built using UITableView
. 我有一个使用UITableView
构建的chatScreen。 I want to scroll to bottom of UITableView
, the moment the screen is opened from some other view controller.However using trivial scroll to bottom functions, shows a jerk if the chat is long.Is there an alternative? 我想滚动到UITableView
底部,从其他视图控制器打开屏幕的那一刻。然而,如果聊天很长,那么使用简单的滚动到底部功能,显示一个混蛋。是否有替代方案?
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
dispatch_async(dispatch_get_main_queue(), ^{
NSIndexPath* lastIndexPath = [NSIndexPath indexPathForRow:_messagesArray.count-1 inSection:0];
[_tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
});
}
使用UITableViewScrollPositionBottom
代替UITableViewScrollPositionTop
。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
dispatch_async(dispatch_get_main_queue(), ^{
CGFloat height = _tableView.contentSize.height - _tableView.bounds.size.height;
[_tableView setContentOffset:CGPointMake(0, height) animated:YES];
});
}
I think this is what you want: 我想这就是你想要的:
extension UITableView {
func scrollToBottom() {
scrollToRow(at: IndexPath(row: numberOfRows(inSection: numberOfSections-1)-1, section: numberOfSections-1),
at: .bottom, animated: true)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.