ios - UITableViewCell 中的两个堆叠动态高度标签

标签 ios swift uitableview autolayout uistoryboard

我正在尝试将两个标签堆叠在一个 UITableViewCell 中。我在 tableView 上设置了 AutomaticDimension 和 estimatedRowHeight。 enter image description here

我希望单元格堆叠显示,如果它们都有内容,则具有动态高度。

如果只有一个有内容,我希望它在单元格中居中,或者更确切地说让单元格折叠以仅显示该标签。

enter image description here

我已经尝试了所有我能想到的约束组合,但通常标签不会扩展,或者我最终会在标签应该出现的地方填充空间。

如果标签没有文本,我将设置 label.text = ""

每次设置文本时,我都会在标签上调用 sizeToFit()

什么是正确的约束组合?这是我目前必须开始的。 enter image description here enter image description here

最佳答案

  • 你必须制作两个带有前导、尾随、顶部和底部约束的标签,并且不要添加高度约束

  • 让标签的行数=0

  • 检查标题标签并从尺寸检查器菜单中点击垂直内容拥抱优先级为 250 而不是 251,如屏幕所示

image

  • 在 viewController 中连接您的 tableview 并添加这些行

    self.tableView.rowHeight = UITableViewAutomaticDimension self.tableView.estimatedRowHeight = 44

关于ios - UITableViewCell 中的两个堆叠动态高度标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52538297/

相关文章:

macos - Swift 显示未解析标识符 'UIAlertController' 的使用

objective-c - 在 UITableView 中自定义索​​引/部分栏的字体/背景颜色

ios - 使用 Bitcode 构建一个 iOS 库,以便与 XCode 6 向后兼容。怎么做?

swift - 在遍历已完成的 NSManagedObject 时如何应用谓词?

swift - 使用 NSUserDefaults 存储 Facebook 用户详细信息

ios - Swift: TableView 父类(super class)错误

ios - UITableView 背景清除彩色单元格

ios - 在 iOS 上使用 GCM 获取 'unknown token'

iOS 13 : Swift - 'Set application root view controller programmatically' does not work

ios - 关于核心数据库设计