uitableview单元格无法删除某些约束

时间:2016-01-08 08:05:48

标签: ios uitableview autolayout

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    //    ChoiceArticleCell *cell = (ChoiceArticleCell *)[tableView dequeueReusableCellWithIdentifier:[ChoiceArticleCell reuseIdentifier] forIndexPath:indexPath];
        ChoiceArticleCell *cell = (ChoiceArticleCell *)[tableView dequeueReusableCellWithIdentifier:[ChoiceArticleCell reuseIdentifier] ];
        NSLog(@"dequed cell.image:%@",cell.listMode.imgUrl);
        for (NSLayoutConstraint* constraint in cell.contentView.constraints)
        {
            NSLog(@"constraints:%@",constraint);

        }
        ArticleListModel *articleListModel = [_choiceArticles.articles objectAtIndex:indexPath.row];
        [cell configWith:articleListModel];

        NSLog(@"after set cell.image:%@",articleListModel.imgUrl);
        for (NSLayoutConstraint* constraint in cell.contentView.constraints)
        {
            NSLog(@"constraints:%@",constraint);

        }
        return cell;
    }

enter image description here

单元格动态删除或添加约束。

但是现在当视图出现时,它总是会解释我的不等约束仍然存在并导致冲突,但在我登录cellForRowAtIndexPath时,我只是创建一个没有这个不等约束的单元格,我不知道为什么这个约束会再次添加到单元格的contentView

控制台日志中的

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.
2016-01-08 15:13:40.547 iKM[10241:4003803] 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:0x18953210 V:[UIImageView:0x18896cc0]-(20)-|   (Names: '|':UITableViewCellContentView:0x18897760 )>",
    "<NSLayoutConstraint:0x18953180 V:|-(>=68)-[UIImageView:0x18896cc0]   (Names: '|':UITableViewCellContentView:0x18897760 )>",
    "<NSLayoutConstraint:0x18963f40 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x18897760(82)]>"
)

我可以看到在不相等的约束中导致冲突是0x18953180,但是当我检查创建单元格的日志时,我在设置单元格的内容后删除了0x18953180约束

2016-01-08 15:13:40.436 iKM[10241:4003803] dequed cell.image:(null)
2016-01-08 15:13:40.437 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x189530f0 H:|-(10)-[UILabel:0x18896eb0'文章标题文章标题文章标题']   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.437 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18953120 V:|-(20)-[UILabel:0x18896eb0'文章标题文章标题文章标题']   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.437 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18953150 H:[UILabel:0x18896eb0'文章标题文章标题文章标题']-(105)-|   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.437 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18953180 V:|-(>=68)-[UIImageView:0x18896cc0]   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.437 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x189531b0 V:[UILabel:0x18896eb0'文章标题文章标题文章标题']-(>=10)-[UIImageView:0x18896cc0]>
2016-01-08 15:13:40.437 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x189531e0 H:|-(10)-[UIImageView:0x18896cc0]   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.437 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18953210 V:[UIImageView:0x18896cc0]-(20)-|   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.438 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18953240 UILabel:0x18897210'1542次浏览'.centerY == UILabel:0x18896a90'根据标签推荐#微信#'.centerY>
2016-01-08 15:13:40.438 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18951970 UILabel:0x18897210'1542次浏览'.centerY == UIImageView:0x18896cc0.centerY>
2016-01-08 15:13:40.438 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x189519a0 H:|-(25)-[UILabel:0x18897210'1542次浏览']   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.438 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x189519d0 UILabel:0x18896830'2015'.centerY == UILabel:0x18897210'1542次浏览'.centerY>
2016-01-08 15:13:40.438 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18951a00 H:[UILabel:0x18896830'2015']-(31)-[UIImageView:0x18952ef0]>
2016-01-08 15:13:40.438 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18951a30 V:|-(20)-[UIImageView:0x18952ef0]   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.439 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18951a60 H:[UIImageView:0x18952ef0]-(10)-|   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.439 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18951a90 H:[UILabel:0x18896a90'根据标签推荐#微信#']-(10)-|   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.440 iKM[10241:4003803] after set cell.image:
2016-01-08 15:13:40.440 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x189530f0 H:|-(10)-[UILabel:0x18896eb0'VR 会是下一个改变世界的计算平台吗?']   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.441 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18953120 V:|-(20)-[UILabel:0x18896eb0'VR 会是下一个改变世界的计算平台吗?']   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.441 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18953150 H:[UILabel:0x18896eb0'VR 会是下一个改变世界的计算平台吗?']-(10)-|   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.441 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x189531b0 V:[UILabel:0x18896eb0'VR 会是下一个改变世界的计算平台吗?']-(>=10)-[UIImageView:0x18896cc0]>
2016-01-08 15:13:40.441 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x189531e0 H:|-(10)-[UIImageView:0x18896cc0]   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.441 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18953210 V:[UIImageView:0x18896cc0]-(20)-|   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.441 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18953240 UILabel:0x18897210'59次浏览'.centerY == UILabel:0x18896a90'8分钟前'.centerY>
2016-01-08 15:13:40.442 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18951970 UILabel:0x18897210'59次浏览'.centerY == UIImageView:0x18896cc0.centerY>
2016-01-08 15:13:40.442 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x189519a0 H:|-(10)-[UILabel:0x18897210'59次浏览']   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.442 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x189519d0 UILabel:0x18896830'2015'.centerY == UILabel:0x18897210'59次浏览'.centerY>
2016-01-08 15:13:40.442 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18951a00 H:[UILabel:0x18896830'2015']-(31)-[UIImageView:0x18952ef0]>
2016-01-08 15:13:40.442 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18951a30 V:|-(20)-[UIImageView:0x18952ef0]   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.443 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18951a60 H:[UIImageView:0x18952ef0]-(10)-|   (Names: '|':UITableViewCellContentView:0x18897760 )>
2016-01-08 15:13:40.443 iKM[10241:4003803] constraints:<NSLayoutConstraint:0x18951a90 H:[UILabel:0x18896a90'8分钟前']-(10)-|   (Names: '|':UITableViewCellContentView:0x18897760 )>

显然,0x18953180在从tableView中出列后存在,并在将单元格返回到tableView之前被删除。 。

但是这个约束又被添加到单元格中,我确信不是我的代码再次添加它,原因是什么

1 个答案:

答案 0 :(得分:0)

最后,我发现为什么会发生冲突,实际上是我使用dequeueReusableCellWithIdentifier的方式,我应该使用dequeueReusableCellWithIdentifier:forIndexPath,并重写单元格的prepareForReuse方法,在prepareForReuse中,我应该首先删除约束。

冲突发生的内在原因是,当使用deque cell时,dequed cell可能包含旧单元格的一些约束(旧约束),并且因为单元格的高度是由新单元格的约束决定的(new约束),所以这可能会发生冲突(旧约束和新单元格高度)