将UITableViewCell高度设置为UIImage高度?

时间:2016-03-05 15:30:52

标签: ios swift uiimageview uiimage heightforrowatindexpath

我有一个自定义的TableViewCell,我希望它的高度与UIImage的大小相匹配。 Cell中的UIImageView设置为纵横拟合,我希望UIImageview与它包含的UIImage一样高,宽度等于表视图的宽度(屏幕宽度)。在方法heightForRowAtIndexPath中:我有给定indexPath.row中的单元格包含的图像。单元格的配置方式是,如果我将heightForRowAtIndexPath设置为让500说,则imageview是唯一会被拉伸的视图。

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let image = comics[indexPath.row].image

        return image.size.height + 103
}

103是构成其余单元格高度的点(按钮和标签)(参见sceenshot)。

现在问题是:所有细胞太高了!好像这不够奇怪,有些太短了??? (图片)我已经阅读了很多类似的帖子,但没有一个回答我的问题。 不知何故,image.size.height比imageview中的实际图像大? 我只是希望uiimageview与其中的图像大小相同,方式为AspectFit。

你对此有什么想法吗?如果您有任何进一步的问题请求我! 谢谢! Screenshot

1 个答案:

答案 0 :(得分:4)

非常感谢你们!纵横比的东西做到了:

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
    }
相关问题