UIRefreshControl at Bottom of UICollectionView (lo

2020-07-24 04:13发布

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

}

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.

2条回答
地球回转人心会变
2楼-- · 2020-07-24 05:04

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()
        }
    }
查看更多
看我几分像从前
3楼-- · 2020-07-24 05:13

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.

查看更多
登录 后发表回答