首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

UITableViewCell中嵌入UITableView的约束

2024-12-16 来源:华拓网

在UITableViewCell中嵌入UITableView的时候,引发以下冲突问题:

2019-11-11 19:20:44.785146+0800 dudu_oc_master[11212:370558] [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:0x6000000f82d0 UITableView:0x7fb174989000.height == 204.203>",
    "<NSLayoutConstraint:0x60000000dc70 UIImageView:0x7fb176459e00.top == UITableViewCellContentView:0x7fb176459c60.top + 8>",
    "<NSLayoutConstraint:0x60000000dd10 UILabel:0x7fb176459fd0.top == UIImageView:0x7fb176459e00.top>",
    "<NSLayoutConstraint:0x60000000ddb0 UILabel:0x7fb17645a4f0.top == UIButton:0x7fb17645a240.bottom + 4.5>",
    "<NSLayoutConstraint:0x60000000de50 UIButton:0x7fb173e44090.top == UILabel:0x7fb17645a4f0.bottom + 8>",
    "<NSLayoutConstraint:0x60000000def0 UITableViewCellContentView:0x7fb176459c60.bottom >= UITableView:0x7fb174989000.bottom + 12>",
    "<NSLayoutConstraint:0x60000000df40 UITableView:0x7fb174989000.top == UIButton:0x7fb173e44090.bottom + 8>",
    "<NSLayoutConstraint:0x60000000e0d0 UIButton:0x7fb17645a240.centerY == UILabel:0x7fb176459fd0.centerY>",
    "<NSLayoutConstraint:0x6000000f86e0 UITableViewCellContentView:0x7fb176459c60.height == 85>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000f82d0 UITableView:0x7fb174989000.height == 204.203>

这个问题发生在我进行折叠显示内层tableView的时候(通过更新约束),比较怪异的是居然与UITableViewCellContentView有关,我在项目中没有手动给UITableViewCellContentView设置过高度,估计是系统auto layout过程中生成的。冲突的原因很明显,由于cell复用的原因,复用时UITableViewCellContentView的高度是85,然后将内层的tableView高度约束调整到了204.20,后者明显大于前者,而后者又包含于前者,就引发了冲突。

最后发现解决问题如下:
将内层tableview到contentView的bottom的约束的priority设置得比UITableViewCellContentView.height更要低就可以了。
这样复用时就会优先应用UITableViewCellContentView.height去做布局,但是请记住你更新了内层的tableview高度此时并没有得到体现,需要执行以下代码:

外层tableView.beginUpdates()
外层tableView.endUpdates()
显示全文