UICollectionView底部的UIRefreshControl(加载更多数据) [英] UIRefreshControl at Bottom of UICollectionView (load more data)

查看:106
本文介绍了UICollectionView底部的UIRefreshControl(加载更多数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UICollectionView ,它显示在线数据库中的图像.我先加载10张图像,然后当用户滚动到 UICollectionView 的底部时,我在后台加载另外10张图像并刷新视图.我使用以下方法完成此操作:

I have a UICollectionView which displays images from an online database. I first load 10 images, and then when the user scrolls to the bottom of the UICollectionView I load another 10 images in the background and refresh the view. I accomplish this using the following method :

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    if (self.localFeedContent != nil && self.localFeedContent!.count > 0) {
         if indexPath.item == (self.localFeedContent!.count-1) {
        loadMoreData()
    }
    }

}

现在这可以很好地工作了,但是并没有向用户指示该过程已完成,除非他向下滚动,否则他不会注意到已经添加了新内容.我想实现一个 UIRefreshControl ,它将附加到 UICollectionView 的底部,并模仿与顶部常规 UIRefreshControl 相同的确切行为屏幕上的.我已经在屏幕顶部实现了一个,但是我也不知道如何在屏幕底部实现它.

Now this works perfectly fine, however there is no indication to the user that the process is complete and unless he scrolls further down, he won't notice that new content has been added. I would like to implement a UIRefreshControl which will be attached to the bottom of the UICollectionView and mimic the same exact behaviour as the regular UIRefreshControl on top of the screen does. I have already implemented one on top of the screen, however I am not sure how to do this at the bottom as well.

推荐答案

我写了类似的东西-我在底部返回了另一个只显示 UIActivityIndi​​cator 的单元格.因此,您的数据源将类似于:

I've written something similar - I return another cell at the bottom that just shows a UIActivityIndicator. So your datasource would be something similar to:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return array.count + 1
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if indexPath.item == array.count{
        // return the new UICollectionViewCell with an activity indicator
    }
    // return your regular UICollectionViewCell
}

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    if indexPath.item == array.count{
        // loadMoreData()
    }
}

或者代替在 willDisplayCell 中调用 loadMoreData(),您可以在 cellForItemAtIndexPath 方法中调用它(返回新的 UICollectionViewCell UIActivityIndi​​cator ).任何一种都可以.

Or instead of calling loadMoreData() in willDisplayCell, you could call it in the cellForItemAtIndexPath method (when you're returning the new UICollectionViewCell with a UIActivityIndicator). Either could work.

这篇关于UICollectionView底部的UIRefreshControl(加载更多数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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