gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:37:25 27 4
gpt4 key购买 nike

我有一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com