首页 > 解决方案 > 表视图单元格高度与内部表视图

问题描述

我有一个 tableView,其中每个单元格都包含另一个具有动态行高的 tableView。我的问题是如何设置第一个表的行高以适应内部 tableView 的高度?我正在使用此代码,但它不能正常工作。

var heights: [CGFloat] = []
var loades: [Bool] = []

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

      return self.heights[indexPath.row]

}



 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath) as! EventCell

        cell.date.text = ev.date

        cell.delegate = self
        cell.event = ev

        self.heights[indexPath.row] = cell.tv.frame.height

        if self.loades[indexPath.row] == false{

            self.loades[indexPath.row] = true
            tableView.reloadRows(at: [indexPath], with: .none)
        }

        return cell}

标签: iosswifttableview

解决方案


我希望你可以访问你的tableview.contentSize.height,如果你的tableview是动态的,contentSize获取所有tableview内容的值,这个高度的内容你可以除以你拥有的行数。这只是一个简单的解决方案,有些像这样:

let size = tableView.contentSize.height
let cellSize = size / models.count

其中 models 是在 tableview 内的 tableviewcell 中打印的对象数组

推荐:将单元格内的高度约束放到tableView中,并尝试使用de sizeCell sum刷新

cell.tableViewInsideCell.reloadData()
cell.heightConstraint.constant = tableView.contentSize.height

并设置

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

      return UITableViewAutomaticDimension

}

推荐阅读