[英]Insert last row in UITableView with smooth scrolling
我正在尝试创建一个像聊天机器人这样的应用程序,我使用了带有自定义单元格的UITableView
来满足我的需求。 每当添加新消息时,我都会创建并插入新行,然后滚动到UITableView
的底部。 一切正常,直到某一点,但是当单元格的高度发生变化(我有两种不同类型的单元格)时,动画是混乱的,它无法平滑滚动到末尾并且整个UITableView
闪烁,这不是良好的用户体验。 我尝试了几种方法:
1-将数据添加到数据源数组并重新加载UITableView
,然后滚动到底部。
2-使用insertRowsAtIndexPaths
然后滚动到底部。
他们两个都有相同的滚动问题。 我使用了scrollToRowAtIndexPath
到达UITableView
的底部
我已上传了一个演示应用程序的代码,该代码在此处表示模拟相同的问题,因此很容易理解。 这是该问题的视频。 任何帮助都非常感谢。
此问题可能不会在模拟器上发生,请在设备上运行演示项目。
在阅读所有评论并在聊天中进行讨论之后,我注意到这在iPhone 5C(10.3.3)上正在发生。 我在iPhone 5S(11.3)上运行了演示,但没有出现此问题。 不知道这是否与操作系统有关。
重新加载整个tableview会导致混乱的滚动,而不是在最后一个位置插入行。
在您的操作方法中进行以下更改
- (IBAction)btnLargeCellClicked:(id)sender { // To add large green colored row.
/************ This is the FIRST approach. ***********/
[arrHeight addObject:@"100"];
[arrData addObject:@"1"];
NSIndexPath* ip = [NSIndexPath indexPathForRow:[_myTable numberOfRowsInSection:0] inSection:0];
[_myTable insertRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationFade];
[self scrollTableviewToLastRow];
/************ This is the SECOND approach. ***********/
// [arrHeight addObject:@"100"];
// [_myTable beginUpdates];
// NSIndexPath *row1 = [NSIndexPath indexPathForRow:arrData.count inSection:0];
// [arrData insertObject:@"1" atIndex:arrData.count];
// [_myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1, nil] withRowAnimation:UITableViewRowAnimationFade];
// [_myTable endUpdates];
// [self scrollTableviewToLastRow];
}
和
- (IBAction)btnClicked:(id)sender { // To add small red colored row.
/************ This is the FIRST approach. ***********/
[arrHeight addObject:@"50"];
[arrData addObject:@"1"];
NSIndexPath* ip = [NSIndexPath indexPathForRow:[_myTable numberOfRowsInSection:0] inSection:0];
[_myTable insertRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationFade];
[self scrollTableviewToLastRow];
/************ This is the SECOND approach. ***********/
// [arrHeight addObject:@"50"];
// [_myTable beginUpdates];
// NSIndexPath *row1 = [NSIndexPath indexPathForRow:arrData.count inSection:0];
// [arrData insertObject:@"1" atIndex:arrData.count];
// [_myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1, nil] withRowAnimation:UITableViewRowAnimationFade];
// [_myTable endUpdates];
// [self scrollTableviewToLastRow];
}
希望这可以帮助 :)
reloads cells every time, that makes animation more busy every additional cell. 看到您的源代码之后,可以说每次都重新加载单元格,这会使动画在每个其他单元格上更加忙碌。 为了您的目标,您应该使用另一个系统:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
at the end too 而且,也许最后您也需要
您将需要此,更改如下的第二种方法
[arrHeight addObject:@"50"];
// [_myTable beginUpdates];
NSIndexPath *row1 = [NSIndexPath indexPathForRow:arrData.count inSection:0];
[arrData insertObject:@"1" atIndex:arrData.count];
[_myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1, nil] withRowAnimation:UITableViewRowAnimationNone];
[_myTable reloadRowsAtIndexPaths:@[row1] withRowAnimation:UITableViewRowAnimationNone];
// [_myTable endUpdates];
[self scrollTableviewToLastRow];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.