UITableView 动态单元格高度冲突 - 高度不依赖于标签

UITableView dynamic cell height conflict - height NOT depending on a label

因此,我的 table 视图显示图像。每个单元格基本上都是一个图像,填充了整个单元格 contentView。由于图像具有不同的纵横比,我需要我的单元格根据 table 视图宽度和图像的纵横比调整它们的高度。我已按照 this Ray Wenderlich 教程进行操作,但现在出现约束冲突。通过改变 imageViews 高度约束来调整图像大小,例如myImageViewHeight.constant = tableView.frame.width / aspectRatio

2016-06-16 13:56:25.823 MyApp[92709:5649464] 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:0x7fbcdaf5c190 V:[UIImageView:0x7fbcdaf5c300(303)]>",
"<NSLayoutConstraint:0x7fbcdaf5b460 V:|-(0)-[UIImageView:0x7fbcdaf5c300]   (Names: '|':UITableViewCellContentView:0x7fbcdaf52230 )>",
"<NSLayoutConstraint:0x7fbcdaf5b4b0 V:[UIImageView:0x7fbcdaf5c300]-(0)-|   (Names: '|':UITableViewCellContentView:0x7fbcdaf52230 )>",
"<NSLayoutConstraint:0x7fbcdaf5e550 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fbcdaf52230(100)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fbcdaf5c190 V:[UIImageView:0x7fbcdaf5c300(303)]>

图像视图具有以下约束,并将单元格 contentView 作为父视图。

在 table 视图控制器 class 中,我正在使用

self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension

我也试过了 - 同样的结果。

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

我猜我必须摆脱 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fbcdaf52230(100)]>" 但是如何呢?此外,是否有人知道某种 documentation/tutorials 如何正确创建 table 具有动态内容的视图, 不仅 涵盖在单元格内有一堆标签?如果将图像视图替换为标签,我的代码 可以正常工作 ...

您的实施是正确的,请不要更改您的代码或任何 constraints.Removing 警告很简单,只需 select 高度限制并将其优先级从 1000 降低到 999 即可修复你的warning.If它还会来,告诉我。

这是一个老问题,但我只是为了记录一下。 这里的问题是估计高度 80 低于计算高度 100。 将估计高度更改为 >=100 应该可以修复此警告。