UITableViewCell不起作用,除非我将视图添加到容器视图而不是contentView中

问题描述 投票:1回答:2

背景

这是对此post的引用(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 code供参考。

ios swift swift4.2 nsautolayout pure-layout
2个回答
0
投票

使用此行之后

self.contentView.autoPinEdgesToSuperviewEdges()

一段时间后,我们意识到滚动多个单元格的表格为jittery,并使电话CPU达到100%。

所以我们回过头去掉了上面几行,解决了自动布局问题,然后一切恢复正常。

因此问题是简单的自动布局问题,像元高度问题是红色鲱鱼。

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


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

}

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

© www.soinside.com 2019 - 2024. All rights reserved.