如何在swift 3中实现表视图单元格中的集合视图的分页?

在这里我有布局,其中我的表视图单元格中的一个由集合视图组成,在这我需要实现分页,我无法使用func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)函数现在如何实现分页的表视图任何人都可以帮助我如何实现这个地方的条件,当它到达我的集合视图的最后一个元素,在这里我知道如何实现分页,但如何调用需要重新加载的函数?

这是我用来检测最后一个单元格的函数,并使用下面的函数来重新加载数据

 func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { let obj = items let lastElement = (obj.count) - 1 print(lastElement) if !loadingData && indexPath.row == lastElement { loadingData = true guard let obj = self.listClassModel else { return } if ((obj.searchCriteria.pageSize as? Int) != nil) { self.collectionView.bottomRefreshControl?.beginRefreshing() let viewController = HomeViewController() viewController.loadMoreData() } } func loadMoreData() { DispatchQueue.global(qos: .background).async { guard let obj = self.listClassModel else { return } let totalItems = obj.totalCount let numberOfItems = obj.searchCriteria.pageSize as! Int let float = Float(totalItems)/Float(numberOfItems) print(float) let totalPages = Int(ceil(float)) print(self.count) if totalPages != self.count { self.index += 1 self.count += 1 print(self.count) let batchSize = 10 let sortField = "name" let sortOrder = "ASC" let conditionType = "eq" let categoryId = 1 self.listCategoryDownloadJsonWithURL(listUrl: listUrlWithParameters) } else { self.loadingData = false } DispatchQueue.main.async { // this runs on the main queue self.loadingData = false } } } 

  1. 将表格视图行高设置为自动尺寸

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 44

2.创buildIBOutlet为tableViewCell中的collectionView的高度。 为tableViewCell中的collectionView设置前导,尾随,顶部,底部约束。

3.为tableViewCell下的collectionView创build出口(如objCollectionView),并在cellForRowAt indexPath方法内添加如下代码

func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) – > UITableViewCell {

  let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! tableCell cell.frame = tableView.bounds cell.layoutIfNeeded() cell.objCollectionView.reloadData() cell.objCollectionViewHeightConstraint.constant = cell.objCollectionView.contentSize.height return cell; 

}

这会自动调整tableViewCell的高度取决于它的内容。