如何在swift中重用tableviewCell和autoLayout?

时间:2015-12-10 08:23:22

标签: ios swift autolayout tableviewcell

我有一个tableview单元格,里面有一个imageview和一个按钮。我为imageview和按钮添加了约束。出现tableview并正确显示单元格。但是当重用单元格时,我向下滚动tableView并向上滚动,单元格显示错误?

当我重用单元格时,我添加了新约束,与早期约束存在冲突,我尝试在添加新约束之前删除先前的约束,但它不起作用。这是项目链接:

https://github.com/chengyanan/chat

如果出现自动布局错误,控制台会抛出警告:

2015-12-10 16:34:30.299 Chat[5488:133086] 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:0x7cc5b120 H:[UIImageView:0x7c94daa0(30)]>",
    "<NSLayoutConstraint:0x7cc5a640 H:|-(8)-[UIImageView:0x7c94daa0](LTR)   (Names: '|':UITableViewCellContentView:0x7c96bdb0 )>",
    "<NSLayoutConstraint:0x7ccab9e0 UIImageView:0x7c94daa0.right == UITableViewCellContentView:0x7c96bdb0.right - 8>",
    "<NSLayoutConstraint:0x7cc71dc0 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7c96bdb0(320)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ccab9e0 UIImageView:0x7c94daa0.right == UITableViewCellContentView:0x7c96bdb0.right - 8>

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.
2015-12-10 16:34:30.300 Chat[5488:133086] 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:0x7cc5b120 H:[UIImageView:0x7c94daa0(30)]>",
    "<NSLayoutConstraint:0x7cc59d40 H:[UIImageView:0x7c94daa0]-(3)-[UIButton:0x7c95b8b0'Trgghhh hhhhhhhhhhhbbbbbv...'](LTR)>",
    "<NSLayoutConstraint:0x7cc78640 H:[UIButton:0x7c95b8b0'Trgghhh hhhhhhhhhhhbbbbbv...'(255)]>",
    "<NSLayoutConstraint:0x7ccc99f0 UIButton:0x7c95b8b0'Trgghhh hhhhhhhhhhhbbbbbv...'.right == UIImageView:0x7c94daa0.left - 3>"
)

enter image description here

1 个答案:

答案 0 :(得分:0)

您是否需要始终删除并添加新约束?你是否可以在cellForRowAtIndexPath期间修改UITableViewCell方法中的当前约束,它将正确布局。