UITableView 无限滚动 [英] UITableView infinite scrolling

查看:38
本文介绍了UITableView 无限滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 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:比较表格内容高度和实际滚动位置的委托方法.

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.

编辑(类似这样):

- (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 无限滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆