Uitableview 调用layoutIfNeeded()时,AutoLayout会打断约束

Uitableview 调用layoutIfNeeded()时,AutoLayout会打断约束,uitableview,swift,autolayout,xcode6,Uitableview,Swift,Autolayout,Xcode6,我试图在UITableView中用XCode6使用Swift实现动态高度uitableviewcell 通过以图形方式设置约束,我将单元格布局如下(屏幕截图来自XCode5,因为XCode6上有NDA)。我还将BodyLabel的换行属性设置为“Word Wrap”,并将行号设置为“0”以允许多行 现在,如果我只是在tableView(tableView:UITableView?,cellForRowAtIndexPath:nsindepath?方法中设置单元格的内容,那么我就可以正确地获得动

我试图在
UITableView
中用XCode6使用Swift实现动态高度
uitableviewcell

通过以图形方式设置约束,我将单元格布局如下(屏幕截图来自XCode5,因为XCode6上有NDA)。我还将BodyLabel的换行属性设置为“Word Wrap”,并将行号设置为“0”以允许多行

现在,如果我只是在
tableView(tableView:UITableView?,cellForRowAtIndexPath:nsindepath?
方法中设置单元格的内容,那么我就可以正确地获得动态高度行为

但是,由于我一直在学习在线教程(特别是),我添加了另一种方法来使用
tableView(tableView:UITableView!、heightForRowAtIndexPath:nsindepath!)
确定单元格的高度。 在我接下来的教程中,它告诉我添加
cell.layoutifneed()
,所以我也添加了它

override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {

    var cell = tableView!.dequeueReusableCellWithIdentifier(kCellIdentifier) as GroupFeedCell

    // Configure the cell
    if let frc = self.fetchedResultsController {
        let feed = frc.objectAtIndexPath(indexPath) as GroupFeed
        cell.titleLabel.text = feed.name
        if let message = feed.message {
            cell.bodyLabel.text = message
        }
    }

    // Layout the cell
    cell.layoutIfNeeded()

    // Get the height
    var height : CGFloat = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
    return height
}
但是,当我运行程序时,尽管表格视图仍然正确显示单元格的动态高度,但它显示的错误如下:

2014-07-27 13:59:22.599 FBGroups[4631:979424] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer    to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x17809e780 H:[UILabel:0x14fd12f50'Body Label Contents...']-(8)-|   (Names:    '|':UITableViewCellContentView:0x178185d70 )>",
    "<NSLayoutConstraint:0x17809e7d0 H:|-(8)-[UILabel:0x14fd12f50'Body Label Contents...']   (Names:    '|':UITableViewCellContentView:0x178185d70 )>",
    "<NSLayoutConstraint:0x17809ea50 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x178185d70(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17809e780 H:[UILabel:0x14fd12f50'Body Label Contents...']-(8)-|   (Names:     '|':UITableViewCellContentView:0x178185d70 )>

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.  
2014-07-27 13:59:22.599 FBGroup[4631:979424]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
我试图找出可能出了什么问题,并花了大量的时间在上面,我发现每当我删除
cell.layoutIfNeeded()
方法时,约束错误就会消失

在冲突的约束中,
UIView-enclosed-Layout-Width
似乎不是我添加的约束,除此之外,所有约束对我来说都是无辜的。我试图搜索能够生成
UIView封装布局宽度
约束的内容,但在我的情况下,我无法得到令人满意的解释。我想知道导致此错误消息的原因以及如何解决此问题


另外,有人能解释一下在计算单元格高度时调用
cell.layoutifneed()
方法的目的是什么,以及何时需要?涵盖动态高度
UITableViewCell
的大多数教程都使用此方法,尽管我的程序在没有该方法调用的情况下仍然正确显示单元格,而且每当我尝试使用该方法时,它导致了异常。

冲突的约束可能是因为前两个约束将标签宽度定义为比单元格宽度小16磅,但第三个约束,即
UIView封装布局宽度
希望使标签具有不同的大小


您可以尝试降低内容压缩阻力/拥抱属性的值,以允许标签忽略其固有大小。

在Xcode中,将最底部垂直约束的优先级设置为750(或小于1000的任何值)。

这似乎是苹果的一个缺陷。我在运行时使用这样一种变通方法,添加
width
约束

- (void)configurateBodyLabelConstraint {
    UIScreen *mainScreen = [UIScreen mainScreen];
    CGFloat viewWidth = mainScreen.bounds.size.width;
    CGFloat bodyLabelWidth = viewWidth - 76 - 8;
    NSDictionary *metric = @ {
        @"bodyLabelWidth": [NSNumber numberWithFloat:bodyLabelWidth]
    };

    UILabel *bodyLabel = self.bodyLabel;
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[bodyLabel(bodyLabelWidth)]"
                                                                   options:0
                                                                   metrics:metric
                                                                     views:@ {
                                                                         @"bodyLabel": bodyLabel
                                                                     }];
    [self.contentView addConstraints:constraints];
}
cell.layoutIfNeeded()-无需调用。
创建一个NSLayoutConstraint

我试图修改该值(降低水平拥抱优先级),但他们没有真正解决我的问题。我也有同样的问题。你找到解决办法了吗?会很有帮助的。你能解决这个问题吗?我也有这个问题。你修好了吗?你试过在标签上设置preferredMaximumWidth吗?你能给你的答案添加一些细节吗?@Tarek,请描述一下你的答案,我不明白你的意思。设置什么的优先级?只需在情节提要中选择约束,然后在对象检查器中设置其优先级字段。酷!将最底部垂直约束的优先级设置为750可以解决此问题。