【发布时间】:2016-05-28 18:20:36
【问题描述】:
我正在尝试在我的 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 是一个在两者中都相同的变量。 aspectRation = 图像的宽度/高度。另外,我检查了两个地方收到的高度是否相同,并且它们是相同的值(在运行时)。
有人知道为什么单元格高度设置不正确吗?
【问题讨论】:
-
您必须添加更多详细信息。首先检查控制台,也许你有自动布局冲突。
-
每张图片的大小都一样吗?
-
当涉及自动布局约束时,您应该使用动态 tableView 单元格高度。
标签: ios iphone swift uitableview heightforrowatindexpath