xcode - 将表格 View 行高设置为标签高度

标签 xcode swift height uitableview

我有一个标签和一个图像。

if image != nil {
  cell height = 445
} else {
  //how do i set the cell height according to the label?
}

最佳答案

您应该在 heightForRowAt 中设置 tableView 单元格的高度。此函数返回给定索引路径的单元格高度(正如您可能已经猜到的那样)。在里面你可以使用 return label.frame.height

在 Swift 3 中:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if image != nil {
    return 445
  } else {
    return label.frame.height
  }
}

编辑:在单元格在 cellForRowAt 中初始化之前,您无法使用 cellForRowAt。所以试试这个吧。 它假设图像和标签是 @IBOutlets

关于xcode - 将表格 View 行高设置为标签高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37892037/

相关文章:

iphone - 在 iOS 中解析 PLIST

xcode - Crashlytics dSYM错误

ios - 使用 segue 传递 UIImage

ios - queryForTable viewDidAppear、viewWillAppear 等不适用于 subview

html - html中的背景高度

给开发者的 iPhone 图形设计建议

xcode - 如何设置 Xcode 以运行 OpenCL 代码,以及如何在构建前验证内核

ios - 输出相机帧缓冲区不会出现

html - 如何更改容器高度以匹配图像高度?

html - IE8 Body 未占用 100% 高度