我正在尝试在我的 tableView 中显示具有正确宽高比的图像。我已经预先计算了图像的长宽比,它似乎在实际图像上正常工作,但在它显示的单元格上却不起作用。
单元格中图像的代码:
private var cellImage: UIImage? {
get { return cellImageView.image }
set {
cellImageView?.image = newValue
if let newimage = newValue {
heightConstraint.constant = newimage.size.width / CGFloat(aspectRatio)
}
}
}
tableView中的代码:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return tableView.bounds.size.width / CGFloat(aspectRatio)
}
aspectRatio 是一个在两者中相同的变量。纵横比 = 图像的宽度/高度。另外,我检查发现两个地方收到的高度是相同的,并且它们是相同的值(在运行时)。
有人知道为什么单元高度设置不正确吗?
最佳答案
您可以使用动态单元格高度并使单元格适合其中的 imageView 检查这个教程 https://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift
关于ios - heightForRowAtIndexPath 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37502414/