这里我有一个如下所示的设计,其中我设计了除最后一个单元格之外的所有单元格,其中它有一个 Collection View ,我需要在其中加载数据并且它有分页所以如何为此提供高度当 Collection View 高度根据我的数组计数动态增加时的单元格。任何人都可以帮助我如何动态地给出高度或任何建议/想法来实现这个?
给定以下结构:
TableView
Table view cell
Table view cell
TableViewCell
CollectionView
CollectionViewCell
CollectionViewCell
CollectionViewCell
[...variable number of cells or different cell sizes]
最佳答案
将表格 View 行高设置为自动尺寸
tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 44
在
tableViewCell
中为collectionView
的高度创建 IBOutlet。在中为
.collectionView
设置leading
,trailing
,top
,bottom
约束tableViewCell在
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/