我有一张 1 倍、尺寸为 320x130 的图像。我想将 UItableViewCell 设置为始终具有该图像的比例。我怎样才能做到这一点?我已经尝试过下面的代码,但似乎使图像非常小?
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let ratio = UIScreen.mainScreen().bounds.width / 130
let targetHeight = 130 * UIScreen.mainScreen().scale
return targetHeight / ratio
}
最佳答案
您应该使用tableView.bounds
而不是UIScreen.mainScreen().bounds
,它在您的情况下是相同的,但如果您更改表格 View ,它不会工作。
至于比例,是这样的:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 130.0/320.0 * tableView.bounds.width
}
您可以通过将高度除以宽度来获得比率,然后将其乘以表格 View 的宽度以获得所需的高度。
关于ios - 设置 UITableViewCell 高度与图像比例成比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35949131/