ios - UITableViewCellStyle 字幕自动布局从 iOS 10 到 iOS 11 的变化?

标签 ios swift uitableview ios10 ios11

使用 iOS 11,我可以成功获得自定尺寸 UITableViewCellStyle.subtitle单元格通过设置 numberOfLines 动态调整其高度detailTextLabel 上的属性(property)为 0。

public class SubtitleCell: UITableViewCell {
    public override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)

        self.textLabel?.font = UIFont.preferredFont(forTextStyle: .headline)
        self.detailTextLabel?.numberOfLines = 0
    }
}

我还确保在 tableView 上启用了自动调整大小。
tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension

在 iOS 11 中,只有这两个更改,我得到了一个调整大小的单元格。伟大的!

enter image description here

但是,在 iOS 10 中,使用相同的代码,我的表格 View 单元格不会调整大小。

enter image description here

我知道 iOS 11 将自动调整表格 View 的大小设为默认值。但是,我不相信 Apple 对表格 View 或表格 View 单元格进行了任何其他功能更改。

为什么,即使启用自动调整表格 View 大小,我也无法获得 subtitle单元格样式正确调整大小?当我使用实时 View 检查器时,我在 iOS 11 或 iOS 10 单元格的表格 View 单元格中看不到任何约束。所以,我觉得 Apple 对默认 UITableViewCell 样式进行了基本更改,但我找不到任何对它的引用。

Sample Project

最佳答案

在 iOS 10 中,为了启用自动调整大小功能,您必须继承 UITableViewCell , 并为 Custom 添加约束样式单元格。注意,它不适用于 Subtitle样式单元格,因为 Apple 不让您能够将约束添加到字幕标签和标题到表格 View 单元格的框架。

您还必须为 tableView.estimatedRowHeight 提供原始值。在 iOS 10 中。

您可以使用可用性检查为 SubtitleCell 运行不同的单元配置。适用于 iOS 10 和 11。

关于ios - UITableViewCellStyle 字幕自动布局从 iOS 10 到 iOS 11 的变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46413010/

相关文章:

ios - 如何让数组中的每个字符串成为其自己的 UITableViewCell,每个都在其自己的部分中?

ios - Swift 中具有两个不同 Segue 的一个原型(prototype)单元格

ios - 自定义 UITableViewCell 图像上的圆角

ios - 如何在 CallKit 中使用 CXStartCallAction isVideo 属性和原生视频按钮?

swift - 将 NSGridView 高度设置为其内容 View 的高度

ios - UITableView不会显示数据

ios - 删除之前发布的通知

ios - 快速 SpriteKit SKNode 类

ios - Quickblox 打开图像作为 WhatsApp

iphone - 如何仅在按下 View 顶部的按钮后才开始运行 View ?