ios - 如何在swift 3中的表格 View 单元格中实现 Collection View 的分页?

标签 ios uitableview swift3 uicollectionview

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

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

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
            }

        }
    }

最佳答案

  • 将表格 View 行高设置为自动尺寸

  • 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 的高度。

    关于ios - 如何在swift 3中的表格 View 单元格中实现 Collection View 的分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902441/

    相关文章:

    ios - ObjectMapper toJson 为空

    ios - 返回变量的所有者 - Swift

    ios - swift EXC_BAD_INSTRUCTION

    ios - 为两个自定义 UITableViewCell 设置不同的高度

    ios - swift/iOS : Error to post image to Pinterest

    ios - 使用 UIImageView 设置缩放 UIScrollView

    ios - 错误,核心数据,原因: '*** -[_PFArray objectAtIndex:]: index (2) beyond bounds (2)'

    iphone - UIPickerView 中的多行选择

    ios - tableview 方法的问题 func tableView(tableView : UITableView, editActionsForRowAtIndexPath indexPath : NSIndexPath) -> [UITableViewRowAction]?

    ios - UIBarButton 单击使用 Swift 4.2 设置标志值