[英]uitableview autoupadate while scrolling to top
我想在滚动到顶部时更新Uitableview。 在facebook应用程序中,当我们将uitableview滚动到顶部时,它将显示“upadating”并在表视图的顶部插入新行。 我想在我的应用程序中给出相同的效果。 是否可以使用默认的uitableview方法进行设计,或者我们需要对其进行自定义。 如果有人对此有所了解,请回复。
您可能想要检测用户何时滚动到顶部:
几种方法:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y == 0)
NSLog(@"At the top");
}
来源: 如何让scrollViewDidScrollToTop在UITableView中工作?
或使用:
scrollViewDidScrollToTop:
资料来源: http : //developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollingViewContent/ScrollingViewContent.html
您现在可以根据用户的操作触发事件:即,当用户滚动到顶部时,使用调用UI更改,然后调用更新逻辑。
insertRowsAtIndexPaths:withRowAnimation:
样本和来源: http : //www.mlsite.net/blog/? p = 466
更新逻辑结束后,假设您的数据源现已更新,请调用[tableView reloadData]。 (可选)您可能希望使用deleteRowsAtIndexPaths:withRowAnimation:在删除单元格时添加效果,通知您当前正在更新。
其他方式 :
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
[self fetchMoreMessages];
}
}
您可以使用insertRowsAtIndexPaths:
在表视图的前面插入新行。 例如,
NSIndexPath *indexPathForRow_0_0 = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *indexPathForRow_0_1 = [NSIndexPath indexPathForRow:1 inSection:0];
[yourTableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathForRow_0_0, indexPathForRow_0_1, nil] withRowAnimation:UITableViewRowAnimationTop];
上面的代码在0的索引0和1处插入两行。
注意:您需要自定义dataSource
和delegate
方法以使用这些插入进行调整。 重要的是你需要从numberOfRowsInSection
方法返回正确的值。 这里,在上面的例子中,你的numberOfRowsInSection
方法应该返回yourPreviousNumberOfRows + 2 ,因为我们在这里添加了两行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.