How to implement pagination for collection view in

2019-08-29 08:07发布

问题:

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:

  1. Set table view row height to automatic dimension

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 44

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

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 {

    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;

}

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