UITableViewCell 高度自动布局在 iOS 10 上不起作用 [英] UITableViewCell height auto layout not working on iOS 10

查看:38
本文介绍了UITableViewCell 高度自动布局在 iOS 10 上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a very simple UITableViewController, where I want to set a text on each textLabel of the UITableViewCells.

高度应通过自动布局计算.因此,在我的 viewDidLoad 中,将以下值设置为 tableView 属性:

The height should be calculated by autolayout. Therefore, in my viewDidLoad set following values to the tableView properties:

tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension

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

This is my cellForRow method, where I initialize the default UITableViewCell and set the text to its label.

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
    }

我的问题是,单元格不会根据标签的大小增长.此问题仅在 iOS 10 上出现.在 iOS 11 上,一切都按原样布置.

My problem is, that the cell won't grow according to the size of the label. This issue only appears on iOS 10. On iOS 11 everything gets layed out as it should.

请检查以下屏幕截图:

  • iOS 10

  • iOS 11

2018 年 5 月 24 日

只有 textLabel,如果 estimatedRowHeight 上有实际值,它将起作用.

Having the textLabel only, it will work if there is an actual value on estimatedRowHeight.

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

But having a cell with style .subtitle, and setting a value to the descriptionLabel, the layouting won't work. Same issue: iOS 10 does not work. iOS 11 works (cell resizes according to both labels).

推荐答案

此配置来自您的 viewDidLoad:

tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension

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

...works only on iOS 11. It is an iOS 11 innovation and does not apply to earlier systems.

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

For iOS 10, you must supply an actual estimatedRowHeight value (such as 60) to turn on automatic variable row height calculation.

这篇关于UITableViewCell 高度自动布局在 iOS 10 上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆