ios - 如何在 UICollectionView 中加载更多对象

标签 ios swift

我有一个 Collection View 和要加载的数据。我的目标是当 Collection View 加载时,它只加载前 5 个项目。当我滚动到 Collection View 底部时,它会加载更多 5 个项目,然后再次加载,直到加载所有数据。

// The data to load (It already have 20 item loaded from server)
static var productInfo: [ProductInfo] = []

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MainProductCell.reuseIdentifier, for: indexPath) as? MainProductCell else {
        fatalError("Expected `\(MainProductCell.self)` type for reuseIdentifier \(MainProductCell.reuseIdentifier). Check the configuration in Main.storyboard.")
    }
        // Set data for collection cell
        cell.modelId = String(ProductCollectionViewController.productInfo[indexPath.row].productId)
        cell.modelName = String(ProductCollectionViewController.productInfo[indexPath.row].name)
        cell.modelPrice = String(ProductCollectionViewController.productInfo[indexPath.row].price)
        cell.modelImage = ProductCollectionViewController.productInfo[indexPath.row].image
        cell.objectImageView.downloadedFrom(link: ProductCollectionViewController.productInfo[indexPath.row].image, contentMode: UIViewContentMode.scaleAspectFit)
        cell.modelLink = String(ProductCollectionViewController.productInfo[indexPath.row].remoteStorage)
    }

    return cell
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    let lastItem = ProductCollectionViewController.productInfo.count - 1
    if indexPath.row == lastItem {
        loadMore()
    }
}

func loadMore() {
    print("load more...")
    //How to handle load more here?
}

如何处理loadMore()函数?谢谢。

最佳答案

直到一切顺利

  if indexPath.row == lastItem && !loading {
    loading = true
    loadMore()
}

关于ios - 如何在 UICollectionView 中加载更多对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49597483/

相关文章:

ios - UIViewController 初始加载时高度错误

ios - iOS 7 上的全屏 UIViewController

Swift SpriteKit GameplayKit 检查 GKStateMachine 不等于状态

objective-c - 转发调用 : the return value gets lost

仅一位客户的 iOS 应用程序崩溃

ios - 快速键盘听不到输入声音

ios - Xcode 在代码中看不到字体,但只在 IB 中看到

swift - 在 ARKit 中同时显示相机捕获的图像和场景背景

ios - 重新加载数据时 Collection View 为零

iphone - 如何为现有PDF文件设置所有者密码?