【问题标题】:ios - How to load more object in UICollectionViewios - 如何在 UICollectionView 中加载更多对象
【发布时间】: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() 函数?谢谢。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    直到一切顺利加起来

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

    【讨论】:

      【解决方案2】:

      使用 itemAtIndexIndexPath 确定最后一个单元格不是最好的方法,您应该检查 scrollView 何时滚动到底部。

      var isScrolledToBottomWithBuffer: Bool {
          let buffer = tableView.bounds.height - tableView.contentInset.top - tableView.contentInset.bottom
          let maxVisibleY = tableView.contentOffset.y + self.tableView.bounds.size.height
          let actualMaxY = tableView.contentSize.height + tableView.contentInset.bottom
          return maxVisibleY + buffer >= actualMaxY
      }
      
      override func scrollViewDidScroll(_ scrollView: UIScrollView) {
          if( scrollView.contentSize.height == 0 ) {
              return;
          }
          if isScrolledToBottomWithBuffer {
              self.loadMore()
          }
      }
      

      【讨论】:

        【解决方案3】:

        改进@duytph 的答案

            var isAllowedToFetchNewDataFromBottom = false
        
        ...
        
                var products: [Products] = []
        
                    ...
        
                    collectionView.delegate = self
        
                    ...
        
                    extension AuthorProductsListViewController: UICollectionViewDelegate {
        
                        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                            if scrollView.contentSize.height == 0 {
                                return
                            }
                            if isScrolledToBottomWithBuffer, isAllowedToFetchNewDataFromBottom {
                                fetchNewData() {
                                    self.isAllowedToFetchNewDataFromBottom = false
                                }
                            }
                        }
        
                        func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
                            lastItemCount = pendingProducts.count
        
                            if indexPath.row == lastItemCount - 1 {
                                isAllowedToFetchNewDataFromBottom = true
                            }
                        }
        
                        var isScrolledToBottomWithBuffer: Bool {
                            let buffer = collectionView.bounds.height - collectionView.contentInset.top - collectionView.contentInset.bottom
                            let maxVisibleY = collectionView.contentOffset.y + self.collectionView.bounds.size.height
                            let actualMaxY = collectionView.contentSize.height + collectionView.contentInset.bottom
                            return maxVisibleY + buffer >= actualMaxY
                        }
        
                    }
        

        【讨论】:

          猜你喜欢
          • 2013-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-21
          • 2017-06-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多