uitableview – 当调用layoutIfNeeded()时,AutoLayout会打破限制

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

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

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

然而,由于我沿用了在线教程(特别是this one),我添加了另一种方法来确定具有tableView(tableView:UITableView !,heightForRowAtIndexPath indexPath:NSIndexPath!)的单元格的高度。
在我正在关注的教程中,它告诉我添加cell.layoutIfNeeded(),所以我也添加了。

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.

我试图弄清楚可能出了什么问题,并花费大量的时间,我想出每当我删除cell.layoutIfNeeded()方法时,约束错误消失。

而且,在相互冲突的约束之中,UIView-Encapsulated-Layout-Width并不是我添加的限制,除此之外,所有约束对我来说都是无辜的。我试图搜索可以生成UIView-Encapsulated-Layout-Width约束的可能性,但是在我的情况下我无法得到令人满意的解释。我想知道这个错误信息的原因是什么,以及如何解决这个问题。

另外,有人可以解释在计算单元格高度之前调用cell.layoutIfNeeded()方法的目的是什么,何时需要?覆盖动态高度UITableViewCell的大部分教程都使用了这种方法,尽管我的程序仍然显示正确的单元格,没有这种方法调用,每当我尝试使用该方法时,它引起了异常。

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

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...