UICollectionView 底部的 UIRefreshControl(加载更多数据)

UIRefreshControl at Bottom of UICollectionView (load more data)

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

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 完全相同的行为。我已经在屏幕顶部实现了一个,但是我也不确定如何在屏幕底部执行此操作。

我写过类似的东西 - 我 return 底部的另一个单元格只显示 UIActivityIndicator。所以你的数据源类似于:

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 方法中调用它(当您 return 使用新的 UICollectionViewCellUIActivityIndicator)。两者都可以。

你不需要在底部实现一个UIRefreshControl,只需要实现一个scrollView方法来在滚动到达屏幕底部时做一些动作。您可以在条件语句中使用一些标志来获得更具体的操作。

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