UIRefreshControl at Bottom of UICollectionView (load more data)

倖福魔咒の 提交于 2019-12-04 15:12:33

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()
    }
}

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.

You don't need to implement a UIRefreshControl on the bottom, just implement a scrollView method to do some action when the scroll reach the bottom of screen. You can use some flags inside the conditional to more get a more specific action.

override func scrollViewDidScroll(scrollView: UIScrollView) {
        let threshold = 100.0 as CGFloat!
        let contentOffset = scrollView.contentOffset.y
        let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
        if (maximumOffset - contentOffset <= threshold) && (maximumOffset - contentOffset != -5.0) {            //Add ten more rows and reload the table content.

            runMoreAPI()
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!