Swift UITableView无法正确计算内容高度

时间:2019-02-02 22:45:14

标签: ios swift uitableview

我在UITableView中遇到了一个奇怪的问题,它无法正确计算内容的高度。

我有一个自定义UITableView类,该类嵌入在另一个自定义UITableView中,我希望它自动调整其高度以适合内容,所以我已经:

override var contentSize: CGSize {
    didSet {
        self.invalidateIntrinsicContentSize()
    }
}

override var intrinsicContentSize: CGSize {
    self.layoutIfNeeded()
    return self.contentSize
}

现在我使用时:

self.estimatedRowHeight = UITableView.automaticDimension // non-zero value like 40 isn't working either
self.rowHeight = UITableView.automaticDimension

输出是不是全高的框架,当我在此TableView中打开“滚动启用”时,它可以滚动显示全部内容(不需要): estimatedRowHeight set to .automaticDimension

现在当我改变

self.estimatedRowHeight = UITableView.automaticDimension

收件人:

self.estimatedRowHeight = 0

除了内容文本被剪切外,输出正是我想要的... estimatedRowHeight = 0

下面是我的CommentCell: CommentCell

控制台在任何情况下都不会显示自动布局错误。

您也许知道发生了什么事?我花了几天的时间试图使这些评论生效,这是我需要的最后一件事。 如果您需要任何更多的信息,请告诉我。

编辑: 如果我将估计的行高度更改为较大的数字(例如500),则会得到单元格下方的空白空间负载: enter image description here 所以它看起来像TableView中不能修复单元格高度的内容。也许这会帮助某人。

1 个答案:

答案 0 :(得分:0)

也许是关于CellView内部的文本字段。您是否将“布局”设置为自动换行?

我还要尝试在“大小”检查器中将其固有大小值设置为“占位符”。