我有一个自定义的 TableViewCell,我希望它的高度与 UIImage 的大小相匹配。 Cell 中的 UIImageView 设置为 aspect fit,我希望 UIImageview 与它包含的 UIImage 一样高,并且宽度等于表格 View 的宽度(屏幕宽度)。在 heightForRowAtIndexPath: 方法中,我有给定 indexPath.row 处的单元格包含的图像。单元格是这样配置的,如果我将 heightForRowAtIndexPath 设置为 500,imageview 是唯一会被拉伸(stretch)的 View 。
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let image = comics[indexPath.row].image
return image.size.height + 103
}
103 是构成单元格其余部分高度(按钮和标签)的点(见截图)。
现在的问题是:所有单元格都太高了!好像这还不够奇怪,其中一些太短了??? (图片)而且我已经阅读了很多类似的帖子,但没有一个能回答我的问题。 不知何故 image.size.height 比 imageview 中的实际图像大? 在 AspectFit 模式下,我只希望 uiimageview 与其中的图像大小相同。
您对此有什么想法吗?如果您还有任何问题,请问我! 谢谢! Screenshot
最佳答案
非常感谢你们!宽高比的东西做到了:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let image = comics[indexPath.row].image
let aspectRatioImg = image.size.height / image.size.width
return view.bounds.width * aspectRatioImg + 103
}
关于ios - 将 UITableViewCell 高度设置为 UIImage 高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35815951/