问题描述
||
我通过提供xml输入从.net Web服务器获取带有消息的图像。
很好。
我每3秒发送一次请求,如果有任何新消息和图像,我只需将这些消息和图像添加到数组中并重新加载表视图即可。
那也很好,但是我需要的是当我重新加载表格视图时,只要有新消息通过平滑滚动现有行就会显示在表格视图上。
和twitter一样。
谁能帮帮我吗。
预先谢谢你。
解决方法
如果您知道新添加的行的“ 0”,则可以使用以下功能(“ 1”)滚动到带有动画的新行(平滑效果)。
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
,您应该调用insertRowsAtIndexPaths:withRowAnimation而不是完全重新加载表:
第二个选项可以让您指定动画效果
(与每次重新加载所有数据相比,这将更加高效)
您可以执行以下操作:
(这假设在将数据加载到数据源对象中之后将调用recievedImageFrom服务器。此外,myDataSourceArray是用于表存储数据的数组,而myTableView是UITableView。
-(void)recievedImageFromServer
{
NSUInteger row = [myDataSourceArray count]-1; // This can also be set to 0 if you want
// to insert at the top of the table
NSIndexPath* newIndex = [NSIndexPath indexPathWithIndex:row];
[myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndex]
withRowAnimation:UITableViewRowAnimationTop];
}
然后它将从其数据源请求单元。