首页 > 解决方案 > UITableViewCell 中的 UICollectionView — 动态高度计算不正确

问题描述

我在表格视图单元格中有一个动态高度集合视图。我在本主题中使用了@Igor 方法。 UITableViewCell 内的 UICollectionView - 动态高度?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "DraftCustomerTableViewCell", for: indexPath) as! DraftCustomerTableViewCell
      cell.data = notificationDraftListViewData[indexPath.section]
      cell.frame = tableView.bounds
      cell.collectionView.reloadData()
      cell.collectionViewHeightConstraint.constant = cell.collectionView.collectionViewLayout.collectionViewContentSize.height
      cell.layoutIfNeeded()
}

 
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        dict[indexPath] = cell.frame.size.height
        
}
    
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

   return dict[indexPath] ?? CGFloat(450)
}

问题是当我第一次展开 tableview 部分时,单元格的高度计算不正确。第一次展开后,collectionView 的高度可以正确计算。我尝试在不点击按钮的情况下让我的 data[0].isExpand 为真,并发现表格视图的单元格高度是正确的。请看演示。有什么方法可以避免这个问题吗?谢谢。

演示:https ://youtu.be/YJCJChCU0wQ

标签: swiftuitableviewuicollectionview

解决方案


推荐阅读