如何在swift 3中的表视图单元格中为集合视图实现分页? [英] How to implement pagination for collection view in table view cell in swift 3?

查看:21
本文介绍了如何在swift 3中的表视图单元格中为集合视图实现分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here I am having layout in which one of my table view cell consists of collection view and in this I need to implement pagination and I am unable to use func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) this function now how to implement pagination for table view can anyone help me how to implement this to place condition when it reaches last element of my collection view and here I know how to implement pagination but how to call the function which needs to reload ?

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

Here is my function which used to detect the last cell and make to reload the data by using below function

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 = UITableViewAutomaticDimensiontableView.estimatedRowHeight = 44

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 44

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

2.Create IBOutlet for the height of collectionView inside tableViewCell. Set leading, trailing, top, bottom constraint for collectionView inside tableViewCell.

3.在tableViewCell下为collectionView创建outlet(如objCollectionView)并在cellForRowAt indexPath方法中添加如下代码

3.Create outlet for collectionView under tableViewCell (like objCollectionView) and add following code inside cellForRowAt indexPath method

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

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的高度.

This will automatically adjust the height of tableViewCell depends on the content it.

这篇关于如何在swift 3中的表视图单元格中为集合视图实现分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆