问题描述
如何在 UITableView
中进行无限滚动?我知道如何使用 UIScrollView
来完成它,其中苹果在WWDC的一个视频中演示过。我尝试在 tableView中执行以下操作:cellForRowAtIndexPath:
:
How do I do an infinite scrolling in a UITableView
? I know how to do it using a UIScrollView
, in which apple has demonstrated in one of the WWDC's video. I tried doing the following in tableView:cellForRowAtIndexPath:
:
if (indexPath.row == [self.newsFeedData_ count] - 1)
{
[self.newsFeedData_ addObjectsFromArray:self.newsFeedData_];
[self.tableView reloadData];
}
但这失败了。还有其他想法吗?
but this fails. Any other idea?
推荐答案
如果您需要知道何时触及UITableView的底部,请成为它的委托(因为它是UIScrollView的子类,并使用-scrollViewDidScroll:delegate方法来比较表的内容高度和它的实际滚动位置。
If you need to know when you hit the bottom of the UITableView, become it's delegate (because it is a subclass of UIScrollView), and use the -scrollViewDidScroll: delegate method to compare the table's content height and it's actual scroll position.
EDIT(类似这样):
EDIT (something like this):
- (void)scrollViewDidScroll:(UIScrollView *)scrollView_
{
CGFloat actualPosition = scrollView_.contentOffset.y;
CGFloat contentHeight = scrollView_.contentSize.height - (someArbitraryNumber);
if (actualPosition >= contentHeight) {
[self.newsFeedData_ addObjectsFromArray:self.newsFeedData_];
[self.tableView reloadData];
}
}
这篇关于UITableView无限滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!