如何根据swift 3 中的集合视图高度动态增加table 视图高度?

how to increase table view height dynamically according to collection view height in swift 3?

这里我有一个如下所示的设计,其中我设计了除最后一个单元格之外的所有单元格,其中它有一个集合视图,我需要在其中加载数据并且它有分页所以如何给出高度对于此单元格,当集合视图高度根据我的数组计数动态增加时。任何人都可以帮助我如何动态地给出高度或任何 suggestions/ideas 来实现这个?

给定以下结构:

TableView

Table view cell

Table view cell

TableViewCell

CollectionView

CollectionViewCell

CollectionViewCell

CollectionViewCell

[...variable number of cells or different cell sizes]

  1. 将table视图行高设置为自动尺寸
    tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 44

  2. tableViewCell 内为 collectionView 的高度创建 IBOutlet。为 tableViewCell 内的 collectionView 设置 leadingtrailingtopbottom 约束。

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