【发布时间】:2013-11-26 19:08:10
【问题描述】:
这是我使用的代码:
//inserting a row at the bottom first
_numberOfRecords++;
[_tableView beginUpdates];
[_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_numberOfRecords-1 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
[_tableView endUpdates];
//clear text
_inputField.text = @"";
//then scroll to bottom
CGPoint bottomOffset = CGPointMake(0, _tableView.contentSize.height + 44.0 + _tableView.contentInset.top - _tableView.bounds.size.height);
NSLog(@"%f", _tableView.contentSize.height + 44.0 + _tableView.contentInset.top - _tableView.bounds.size.height);
[_tableView setContentOffset:bottomOffset animated:YES];
这将以一种非常奇怪的方式滚动 tableview。 但是,如果我在插入之前放置滚动代码,它可以正常工作,只是它忽略了最新插入的行。也就是说,它滚动到倒数第二行,而不是滚动到最后一行(当然,因为它在插入新卷之前滚动。)
所以我相信这段代码应该滚动到的位置没有问题。 问题可能来自对 tableview 的行插入。 它违反了滚动表格视图的动画。
我这样做是为了创建一个聊天视图。 每次用户发送或接收消息时,我都会在表格视图中插入一行包含该消息的行,并将其滚动到底部。这就是我在这里使用 tableView 的原因。我尝试使用带有标签的scrollView,效果很好,但tableView 在聊天视图中似乎更受欢迎。
本来想用scrollView或者tableView,发现苹果内置的消息app用的是tableView,所以采用tableView。让我知道带有 Label 的 scrollView 是否比 tableView 更好。
无论如何,如何在插入新行后将 tableView 滚动到底部?
【问题讨论】:
-
你不能使用'scrollToRowAtIndexPath: atScrollPosition: animated:'
标签: ios uitableview uiscrollview livechat