我正在尝试在我的 tableView 中显示具有正确纵横比的图像。我已经预先计算了图像的纵横比,它似乎在实际图像上正常工作,但在它显示的单元格上却不行。
单元格中的图像代码:
private var cellImage: UIImage? {
get { return cellImageView.image }
set {
cellImageView?.image = newValue
if let newimage = newValue {
heightConstraint.constant = newimage.size.width / CGFloat(aspectRatio)
}
}
}
表格视图中的代码:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return tableView.bounds.size.width / CGFloat(aspectRatio)
}
aspectRatio 是一个在两者中都相同的变量。aspectRation = 图像的宽度/高度。另外,我检查了两个地方收到的高度是相同的,并且它们是相同的值(在运行时)。
有人知道为什么单元格高度设置不正确吗?