【问题标题】:Dynamic height of tableview cell on basis of UICollectionView cell inside it基于其中的 UICollectionView 单元格的 tableview 单元格的动态高度
【发布时间】:2021-11-25 11:11:27
【问题描述】:

我有tableview单元格,里面有collectionview,还有collectionview单元格,如何根据collectionview单元格设置tableview单元格的高度?

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

    尝试将其添加到您的控制器中

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableView.automaticDimension
        }
        
        func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableView.automaticDimension
        }
    

    【讨论】:

      【解决方案2】:

      你的问题很模糊。

      你想提供一个你想要达到的目标的例子。

      假设您有集合视图单元格的静态高度: 简单,自动布局将成为您的朋友。 使用:

      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
              return UITableView.automaticDimension
          }
          
          func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
              return UITableView.automaticDimension
          }
      

      但是,如果您想动态布局表格视图单元格高度:

      1. 为集合视图单元格的最小高度设置高度约束
      2. 计算内容大小:
      
      // You may need to call it when viewDidLayoutSubviews() is called
      let height = collectionView.collectionViewLayout.collectionViewContentSize.height
      collectionViewHeightConstraint.constant = height
      contentView.layoutIfNeeded()
      

      【讨论】:

        猜你喜欢
        • 2018-09-30
        • 1970-01-01
        • 1970-01-01
        • 2017-11-14
        • 1970-01-01
        • 1970-01-01
        • 2020-12-27
        • 2015-03-25
        • 1970-01-01
        相关资源
        最近更新 更多