在这里,我有一个布局,其中我的表视图单元格之一由集合视图组成,在这个布局中我需要实现分页,但我无法使用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
}
}
}
- 将表视图行高设置为自动尺寸
tableView.rowHeight = UITableViewAutomaticDimension
表视图.estimatedRowHeight = 44
2.根据tableViewCell内collectionView的高度创建IBOutlet。为tableViewCell内的collectionView设置前导、尾随、顶部、底部约束。
3.在tableViewCell(如objCollectionView)下为collectionView创建outlet,并在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 的高度。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)