【问题标题】:How to implement pagination for collection view in table view cell in swift 3?如何在swift 3中的表格视图单元格中实现集合视图的分页?
【发布时间】:2018-06-02 19:14:17
【问题描述】:

在这里我有一个布局,其中我的一个表格视图单元格由集合视图组成,在这个我需要实现分页,我现在无法使用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
            }

        }
    }

【问题讨论】:

  • 你找到解决办法了吗?

标签: ios uitableview swift3 uicollectionview


【解决方案1】:
  1. 将表格视图行高设置为自动标注

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 44

2.为tableViewCell内collectionView的高度创建IBOutlet。为 tableViewCell 中的 collectionView 设置前导、尾随、顶部、底部约束。

3.在tableViewCell下为collectionView创建outlet(如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的高度。

【讨论】:

  • 现在我需要在表格视图单元格中实现集合视图的分页,我如何检查集合视图最后一项的条件并需要重新加载如何放置它我问@Hemant Sabale
  • 是的,它帮助我在上一个问题中标记为答案
  • 我的问题解决了增加表格视图单元格的限制,但如果可能的话,分页没有解决,请帮助我兄弟
  • 什么是分页类型?水平或垂直。
  • 在我给定的网址中,只有十个项目必须从服务中加载,再次调用后第二组项目将被加载
猜你喜欢
  • 2018-07-19
  • 1970-01-01
  • 2018-07-23
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多