systemLayoutSizeFittingSize 没有为我的 UILabel 返回足够大的尺寸

systemLayoutSizeFittingSize not returning a size big enough for my UILabel

我正在使用静态 Table 视图来显示一些数据,因为我的数据是动态的,所以我需要一个可以根据内容调整大小的单元格。我已经覆盖 heightForRowAtIndexPath:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == 4 {
        let size = itemDescription.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
        return size.height
    }
    return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

我已经尝试了 UILayoutFittingCompressedSizeUILayoutExpandedSize,两者都会 return 一个 CGSize 高度为 140,这不足以显示我的文字。

我也试过增加UILabel中的行数,但也没用。

在故事板上,我的 Cell 是一个基本的 TableViewCell

有人知道发生了什么事吗?

要使用自动调整大小的单元格,您不需要覆盖 heightForRowAtIndexPath。覆盖 estimatedHeightForRowAtIndexPath 和 return 估计值或 UITableViewAutomaticDimension。而且,重要的是,您必须对单元格的所有 4 个边都有明确的约束。对于 UILabel 指定 0 作为行数以启用文本自动高度。