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

标签 ios swift autolayout tableviewcell

我使用静态 TableView 来显示一些数据,因为我的数据是动态的,所以我需要一个可以根据内容调整大小的单元格。我已经覆盖了 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,两者都会返回一个 CGSize,高度为 140,不足以显示我的文本。

我还尝试增加 UILabel 中的行数,但它也不起作用。

在 Storyboard 上,我的 Cell 是一个基本的 TableViewCell

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

最佳答案

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

关于ios - systemLayoutSizeFittingSize 没有为我的 UILabel 返回足够大的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31974959/

相关文章:

ios - startMonitoringSignificantLocationChange 不会调用 didUpdateLocations

ios - 如何在TabBarController中重用系统图标?

macos - OSX Dropbox Sync API 错误 - 无法验证链接请求

swift - 我似乎无法解决 valueForUndefinedKey :]: whereto class is not key value coding-compliant 的错误

ios - UICollectionView 作为 subview 不更新约束

ios - 如何将标签/UIView 固定到 ARKit 中的节点

iOS:如何在弹出 UIViewController 的子 UIViewController 后将其返回 "Refresh"?

json - 从 NSURLConnection 加载 UITableView 时数据加载时间过长

iphone - 具有自动布局的 UIScrollView 无法按预期工作

ios - 当推送每个新的 View Controller 时,调整以模态 UIModalPresentationFormSheet 呈现的导航 Controller 的大小