1

背景

这是参考这篇文章(TLDR;如果您正确使用自动布局,则应自动计算 UITableViewCell 高度)。

问题

如果我将视图直接添加到contentView,我会不断收到此错误:

018-11-25 02:17:33.514881+0200 [78571:855018] [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:0x2825f0be0 V:|-(76)-[.TTRLabel:0x10e7c7e40'DRIVER']   (active, names: '|':UITableViewCellContentView:0x10e7cb700 )>",
    "<NSLayoutConstraint:0x2825f0b90 V:[.TTRLabel:0x10e7c7e40'DRIVER']-(10)-[.TTRLabel:0x10e7c8420'TIME']   (active)>",
    "<NSLayoutConstraint:0x2825f1c70 V:[.TTRLabel:0x10e7c8420'TIME']-(10)-[.TTRLabel:0x10e7c8a00'ADDRESS']   (active)>",
    "<NSLayoutConstraint:0x2825f1ea0 .TTRLabel:0x10e7c96a0' new office '.bottom == UITableViewCellContentView:0x10e7cb700.bottom - 26   (active)>",
    "<NSLayoutConstraint:0x2825f1f40 .TTRLabel:0x10e7c96a0' new office '.lastBaseline == .TTRLabel:0x10e7c8a00'ADDRESS'.lastBaseline   (active)>",
    "<NSLayoutConstraint:0x2825f34d0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x10e7cb700.height == 44   (active)>"
)

我的问题是这部分

<NSLayoutConstraint:0x2825f34d0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x10e7cb700.height == 44   (active)>"

它坚持 tableViewCell 高度只是标准的 44 (因此忽略了我所有的自动布局工作!

唯一能解决问题的就是运行这个

self.contentView.autoPinEdgesToSuperviewEdges()

这很奇怪,b/c 它没有记录在任何地方,并且在宏伟的计划中没有意义。

这是整个UITableViewCell 代码供参考。

4

2 回答 2

0
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

}

// 我添加了这段代码,它对我有用,我的问题是在 iOS 10 上

于 2020-05-12T10:06:19.570 回答
0

使用这条线后

self.contentView.autoPinEdgesToSuperviewEdges()

有一段时间,我们意识到滚动带有许多单元格的表格很紧张,并使手机 CPU 达到 100%。

所以我们回去删除了上面的行,修复了自动布局问题,然后事情又恢复了。

所以问题是简单的自动布局问题,单元格高度是一个红鲱鱼。

TL;DR:如果您正确使用自动布局,则会自动计算UITableViewCell高度。

于 2019-02-04T04:17:37.633 回答