如何在Swift 3中根据集合视图高度动态增加表格视图高度?

3
在下面的设计中,除了最后一个单元格中的集合视图之外,我已经设计好了所有单元格。在这个集合视图中,我需要加载数据,它具有分页功能,所以当集合视图高度根据我的数组计数动态增加时,如何为该单元格提供高度?有人能帮助我动态地设置高度或提供实现的建议/想法吗?
给出以下结构:
TableView TableViewCell TableViewCell TableViewCell CollectionView CollectionViewCell CollectionViewCell CollectionViewCell [...不同数量或不同大小的单元格]

尝试使用以下代码:self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 44.0; // 设置平均预估高度 - Priya
但是集合视图增加了表格视图单元格高度,就像你说的那样@Priya。 - User
在表格视图单元格内添加所有必需的约束条件,它将根据内容自动增加高度。 - Priya
你是否尝试通过嵌入集合视图并给表格视图单元格添加约束来解决问题?这样不会增加表格视图单元格的高度。@Priya - User
1个回答

8
  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;
    
    }
    
这将自动根据内容调整tableViewCell的高度。

我需要为表视图单元格内的集合视图实现分页,这可能吗? - User
你遇到了什么问题? - Hemant Sabale
你是否已经为tableViewCell添加了正确的约束?同时,你是否设置了tableView.rowHeight = UITableViewAutomaticDimension和tableView.estimatedRowHeight = 44? - Hemant Sabale
谢啦兄弟,我忘了在 my height for row at index path 方法中将高度更改为 UITableViewAutomaticDimension。 - User
我在第一次加载时遇到了布局单元格高度的问题,现在通过添加以下代码已经解决了:cell.frame = tableView.bound感谢您节省了我的时间。 - Purushottam Padhya
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接