UITableViewCell高度自动布局无法在iOS 10上运行

kch*_*mik 3 uitableview uikit swift

我有一个非常简单的UITableViewController,在这里我想设置一个文本上的每个textLabel中的UITableViewCell秒.

高度应通过autolayout计算.因此,在我viewDidLoadtableView属性中设置以下值:

tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)

这是我的cellForRow方法,我初始化默认值UITableViewCell并将文本设置为其标签.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.numberOfLines = 0
        cell.textLabel?.text = "hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello"
        return cell
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是,细胞不会根据标签的大小而增长.此问题仅出现在iOS 10上.在iOS 11上,所有内容都应该布局.

请检查以下屏幕截图:

  • iOS 10

在此输入图像描述

  • iOS 11

在此输入图像描述

编辑:2018年5月24日

拥有textLabel唯一的,如果有实际值,它将工作estimatedRowHeight.

但是如果有一个带有样式的单元格.subtitle,并为其设置值descriptionLabel,则布局将不起作用.同样的问题:iOS 10不起作用.iOS 11可以工作(根据两个标签调整单元格大小).

mat*_*att 16

来自您的配置viewDidLoad:

tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)

...仅适用于iOS 11.这是iOS 11的创新,不适用于早期的系统.

对于iOS 10,您必须提供实际estimatedRowHeight值(例如60)以打开自动变量行高计算.

  • @kchromik最简单的解决方案是:不要使用内置的“.subtitle”样式。它不适用于自调整大小的单元,因此它不能以向后兼容的方式工作。只需创建一个带有两个标签的自定义单元格类型即可。这适用于 iOS 10 和 iOS 11。 (2认同)