【发布时间】:2020-03-11 01:13:22
【问题描述】:
我有一个要加载的集合视图和数据。我的目标是当集合视图加载时,它只加载前 5 个项目。当我滚动到集合视图的底部时,它会再次加载 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() 函数?谢谢。
【问题讨论】: