我有一个标签和一个图像。
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/