【问题标题】:How to tell UITableView that a self-sizing cell's height is about to change?如何告诉 UITableView 自调整单元格的高度即将改变?
【发布时间】:2015-01-21 04:22:43
【问题描述】:

我有一个 UITableView,它的内容有些动态。似乎每次我调整它们的大小时,我都会遇到这个错误:

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:0x15dbabf0 WKWebView:0x15da1650.top == UITableViewCellContentView:0x15eaedb0.topMargin>",

    "<NSLayoutConstraint:0x15dbac20 UITableViewCellContentView:0x15eaedb0.bottomMargin == WKWebView:0x15da1650.bottom>",

    "<NSLayoutConstraint:0x15db1360 V:[WKWebView:0x15da1650(158)]>",

    "<NSLayoutConstraint:0x15ed0c00 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x15eaedb0(43.5)]>"

)

最后一个——标记为UIView-Encapsulated-Layout-Height 的那个——破坏了一切。我想摆脱它,或者将其设置为破坏优先级,或者其他什么 - 但我似乎无法告诉 UITableView 破坏它。我已经在各种视图上尝试了setNeedsUpdateConstraintssetNeedsLayout 的所有可能组合。我尝试从updateConstraints 上的单元格contentView 中删除所有约束。我试过只调用 beginUpdatesendUpdates - 在这种情况下,永远不会调用 updateConstraints 并且不会重新评估约束。

在这里,我不知所措。如果没有自调整单元格,只需调用 begin/endUpdates 来触发重新计算单元格的尺寸。我现在该怎么做?

【问题讨论】:

  • 您需要根据需要正确设置单元格的约束,然后需要正确计算高度..
  • @TamilKing 我说的是 iOS 8 中的自定大小表格视图单元格。请观看 WWDC 2014 的表格和集合视图中的新功能视频。跨度>
  • 删除单元格并使用新合同将其添加回来,并使用 heightForRowAtIndexPath 给出单元格的高度
  • @ShoaibSarwarCheema 正如我上面所说的 - 我说的是自调整大小的表格视图单元格。此外,我尝试重新加载单元格并为 UITableView 提供相同的单元格实例 - 同样的问题。在这种情况下,单元格的大小来自 WKWebView,因此这不是一个很好的解决方案。
  • 我在动态表格视图单元格大小方面遇到了类似的问题。我遵循了这个教程:raywenderlich.com/87975/…,它工作得很好,除了当我尝试在我自己的表格视图实现中使用相同的概念时,我无法让它工作。它应该可以工作,除了这个“UIView-Encapsulated-Layout-Height”不正确并且打破了我的所有约束。它不会像我希望的那样根据单元格的布局进行自我调整:(

标签: ios uitableview autolayout


【解决方案1】:

您在内容视图中使用的 WKWebView 是破坏约束的最可能原因。此外,任何 webView 的高度只能在 WebView 完成加载后计算。例如,在 UIWebView 中,只能在 delgate 方法“didFinishLoadingWebView”方法中适当地获取 UIWebView 的高度。

此外,“UIView-Encapsulated-Layout-Height”约束可能不会得到满足,因为计算出的单元格的固有高度可能具有不同的值。因此可以降低相关单元格约束的优先级以允许 UIView-Encapsulated-Layout-Height 具有优先权。

【讨论】:

    猜你喜欢
    • 2015-05-26
    • 2019-08-15
    • 1970-01-01
    • 2020-10-15
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 2017-02-26
    相关资源
    最近更新 更多