ios - 如何根据 swift 3 中的 Collection View 高度动态增加 TableView 高度?

标签 ios uitableview swift3

这里我有一个如下所示的设计,其中我设计了除最后一个单元格之外的所有单元格,其中它有一个 Collection View ,我需要在其中加载数据并且它有分页所以如何为此提供高度当 Collection View 高度根据我的数组计数动态增加时的单元格。任何人都可以帮助我如何动态地给出高度或任何建议/想法来实现这个?

给定以下结构:

TableView

Table view cell

Table view cell

TableViewCell

CollectionView

CollectionViewCell

CollectionViewCell

CollectionViewCell

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

最佳答案

  1. 将表格 View 行高设置为自动尺寸
    tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 44

  2. tableViewCell 中为 collectionView 的高度创建 IBOutlet。在 中为 collectionView 设置 leading, trailing, top, bottom 约束tableViewCell.

  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 中的 Collection View 高度动态增加 TableView 高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47882061/

相关文章:

ios - GKLocalPlayer localPlayer无法验证: "this application is not recognised by Game Center" error GKServerStatusCode 5019

c# - 如何在 Xamarin.Forms 中检测 Stacklayout 的左滑动和右滑动?

swift - 如何初始加载 UITableView 然后观察 Firebase 节点的更改以在 Swift 中刷新 tableview?

swift - 我们如何在其选择项委托(delegate)上重新加载 Collection View ?

ios - Alamofire 和 AlamofireObjectMapper 创建通用函数

ios - iPhone - 为我的应用程序存储数据的最佳方式

ios - 在Xcode 7.3.1 Playgrounds中正确绘制数组

iOS:添加复选标记以选择 TableView 中的特定行

ios - 在 TableViewCell 内的 TextView 中快速点击背线和字母后,TableView 中重复多个部分

ios - 使用 UITextView 进行动态分页