ios - 设置 UITableViewCell 高度与图像比例成比例

标签 ios swift

我有一张 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/

相关文章:

swift - Alamofire 多参数(查询和表单)Swift 4

ios - 带有自定义 selectedBackgroundView 的 UITableViewCell,它比 UISplitViewController 上的主视图 Controller 更宽

swift - 是否可以为协议(protocol)创建仅类扩展?

ios - 为什么DispatchGroup会干扰主队列?

swift - 在 swift 中,如何使函数从 Firebase 返回查询快照?

uitableview - iOS Swift - 使用搜索栏和搜索显示过滤具有自定义单元格的 UITableView 的内容

ios - 快速将 GPS 元数据字典添加到使用 AVFoundation 拍摄的图像

ios - 在 Swift 中将数据源分离到另一个类,然后处理数据更改

ios - 如何在 UIViewController 出现时得到通知?

ios - resizableImageWithCapInsets :resizingMode: crashing on iOS 5. 1