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

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

概述我试图使用Swift在XCode6的UITableView中实现动态高度UITaleViewCell。 我通过以图形方式设置了我的单元格(屏幕截图来自XCode5,因为XCode6上的NDA)。我还将BodyLabel的换行符属性设置为“Word Wrap”,并将行号设置为“0”以允许多行。 现在,如果我只是在tableView(tableView:UITableView?,cellForRowA 我试图使用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)。 总结

以上是内存溢出为你收集整理的uitableview – 当调用layoutIfNeeded()时,AutoLayout会打破限制全部内容,希望文章能够帮你解决uitableview – 当调用layoutIfNeeded()时,AutoLayout会打破限制所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1055374.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存