【问题标题】:How to display image in table view cell with dynamic height?如何在具有动态高度的表格视图单元格中显示图像?
【发布时间】:2018-08-04 12:24:55
【问题描述】:

UITableViewCell 包含 UIImageView 以显示不同宽度/高度比的图像。

表格视图单元格应根据图像大小比例调整其高度。所以图像视图宽度应该等于单元格宽度,图像高度是cell width * (image height / image width),以将图像显示为aspectFit

图像视图添加了自动布局约束:

heightConstraint = imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 1)

NSLayoutConstraint.activate([
    imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
    imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
    imageView.topAnchor.constraint(equalTo: contentView.topAnchor),
    imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
    heightConstraint
])

为单元格设置图像时更新高度约束:

func updateCell(withImage image: UIImage) {

    // Update image
    imageView.image = image

    // Deactivate old height constraint
    NSLayoutConstraint.deactivate([heightConstraint])

    // Activate new height constraint
    heightConstraint = imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: image.size.height / image.size.width)
    NSLayoutConstraint.activate([heightConstraint])
    }
}

单元格高度在设备上看起来是正确的,但它会记录自动布局错误:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x60700016d600 H:|-(15)-[MyApp.MyImageView:0x61600042b180]   (active, names: '|':UITableViewCellContentView:0x615000056e80 )>",
    "<NSLayoutConstraint:0x60700016d590 MyApp.MyImageView:0x61600042b180.trailing == UITableViewCellContentView:0x615000056e80.trailing - 15   (active)>",
    "<NSLayoutConstraint:0x60700016d4b0 V:|-(15)-[MyApp.MyImageView:0x61600042b180]   (active, names: '|':UITableViewCellContentView:0x615000056e80 )>",
    "<NSLayoutConstraint:0x607000071210 MyApp.MyImageView:0x61600042b180.bottom == UITableViewCellContentView:0x615000056e80.bottom - 15   (active)>",
    "<NSLayoutConstraint:0x607000303bb0 MyApp.MyImageView:0x61600042b180.height == 1.499*MyApp.MyImageView:0x61600042b180.width   (active)>",
    "<NSLayoutConstraint:0x607000073430 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x615000056e80.height == 547   (active)>",
    "<NSLayoutConstraint:0x6070000734a0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x615000056e80.width == 375   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x607000303bb0 MyApp.MyImageView:0x61600042b180.height == 1.499*MyApp.MyImageView:0x61600042b180.width   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

添加的高度约束似乎与UITableViewCellContentView 高度冲突。例如,UILabel 不会有这个问题,因为它有一个内在的内容大小,但 UIImageView 的内在内容大小是它的图像大小。那么如果没有像单独计算每个单元格高度这样的解决方法,这里的解决方案是什么?

【问题讨论】:

    标签: ios uitableview uiimageview autolayout


    【解决方案1】:

    您可以通过将 imageView 的底部约束的优先级设置为 999 来防止此日志,因为当单元格加载时,它会建议一个静态高度,该高度与动态 tableView 中总是发生的约束冲突

    【讨论】:

    • 它通过将底部布局约束优先级的优先级设置为.defaultHigh来工作。然后我又遇到了一个布局错误,但是为tableView.estimatedRowHeight 设置一个值也解决了这个问题。
    • 是的,您可以将其设置为1000以外的任何优先级
    • 你能解释一下它是如何工作的吗?如果单元格高度约束优先级为 1000,并且我添加了优先级
    • 是的,它会破坏你的,直到计算出正确的高度并且你的约束和系统结合在一起将允许它并显示正确的外观
    猜你喜欢
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多