如何在 table 视图单元格中以动态高度显示图像?

How to display image in table view cell with dynamic height?

一个UITableViewCell包含一个UIImageView来显示不同宽度/高度比例的图像。

table 视图单元格应根据图像大小比例调整其高度。因此图像视图宽度应等于单元格宽度,图像高度为 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 的固有内容大小是其图像大小。那么,如果没有像单独计算每个单元格高度这样的解决方法,这里的解决方案是什么?

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