【问题标题】:Updating UITableViewCell constraints causes exception更新 UITableViewCell 约束会导致异常
【发布时间】:2014-02-07 02:35:33
【问题描述】:

我将 Xcode 5 与 iOS 7 SDK 一起使用。我有一个 UITableViewCell 定义为我的故事板中的原型,并且我正在对单元格内的子视图使用自动布局。当cellForRowAtIndexPath 请求单元格时,我用其数据填充单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityTableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"Activity"];
    Activity *activity = self.activities[(NSUInteger)indexPath.row];

    [tableViewCell configureWithActivity:activity];

    return tableViewCell;
}

此数据可能会导致其中一个子视图的高度发生变化。我正在通过修改其约束以及其他约束修改来更改子视图的高度:

@implementation ActivityTableViewCell

- (void)updateConstraints
{
    // Update constraints based on the activity
    self.activityDocumentsTableViewHeightLayoutConstraint.constant = ...;
    self.betweenActivityDocumentsTableViewAndNotesLabelLayoutConstraint.constant = ...;
    self.activityNotesTableViewHeightLayoutConstraint.constant = ...;
    self.betweenActivityNotesTableViewAndContentViewLayoutConstraint.constant = ...;

    [super updateConstraints];
}

- (void)configureWithActivity:(Activity *)activity
{
    // Populate subviews using the activity variable

    [self setNeedsUpdateConstraints];
}

@end

我的 ActivityTableViewCell 类派生自另一个类,它告诉内容视图自动调整大小以适应其动态大小的子视图:

self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

我的视图控制器预先计算所有这些单元格的高度并将它们存储在一个数组中。这些高度在heightForRowAtIndexPath 中提供。

所有这些都完美运行,直到我滚动表格视图:

2014-01-17 10:41:47.435 ... 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:0x13c4a520 UnknownLabel:0x13c4a440.height == 26>",
    "<NSLayoutConstraint:0x13c4ad20 BorderedTableView:0xd46e000.height == 54>",
    "<NSLayoutConstraint:0x13c4b800 UnknownLabel:0x13c4b720.height == 26>",
    "<NSLayoutConstraint:0x13c4be40 BorderedTableView:0xd462600.height == 74>",
    "<NSLayoutConstraint:0x13c4cc60 BorderedTableView:0xd46e000.top == UnknownLabel:0x13c4a440.bottom + 8>",
    "<NSLayoutConstraint:0x13c4ccc0 BorderedTableView:0xd462600.top == UnknownLabel:0x13c4b720.bottom + 8>",
    "<NSLayoutConstraint:0x13c4cd20 UITableViewCellContentView:0x13c49de0.bottom == BorderedTableView:0xd462600.bottom + 8>",
    "<NSLayoutConstraint:0x13c4cd50 UnknownLabel:0x13c4a440.top == UITableViewCellContentView:0x13c49de0.top + 20>",
    "<NSLayoutConstraint:0x13c4cd80 UnknownLabel:0x13c4b720.top == BorderedTableView:0xd46e000.bottom + 55>",
    "<NSAutoresizingMaskLayoutConstraint:0xb2c1bf0 UITableViewCellContentView:0x13c49de0.height == UITableViewCellScrollView:0x13c4d000.height>",
    "<NSAutoresizingMaskLayoutConstraint:0xb2c2460 UITableViewCellScrollView:0x13c4d000.height == ActivityTableViewCell:0x13c49be0.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x13935110 ActivityTableViewCell:0x13c49be0.height == 318>"
)

请注意,所有垂直常数的总和不是总计 318,因此例外。 318 来自我预先计算的高度,是这个单元格的正确高度。但是,该异常中的第四个约束 "&lt;NSLayoutConstraint:0x13c4be40 BorderedTableView:0xd462600.height == 74&gt;" 是不正确的(它在第一次出列和配置此单元格时是正确的)。这是因为 iOS 似乎在调用我的单元格的 updateConstraints 方法之前应用了行高,该方法本身将第四个约束更新为正确的值。

我尝试了多种调用updateConstraintssetNeedsLayout等的组合,但均无济于事。我根本无法弄清楚如何防止 iOS 在更新我的约束之前更新自动调整大小的掩码之前。好消息是 updateConstraints 被调用之后——而且最终是——约束似乎起作用了;我正确地看到了我的单元格,并且所有子视图的大小都正确。我只是不喜欢在控制台中看到这些异常,如果可能的话,我更愿意消除它们。

【问题讨论】:

  • 类似于stackoverflow.com/questions/36929221/…。你有没有解决过这个问题?
  • 我已经有一段时间没有使用该应用程序了,但是没有,我从未找到合适的解决方法。 :(

标签: ios constraints tableview autolayout


【解决方案1】:

除了在垂直约束之一上将优先级更改为 999 之外,我真的没有找到任何干净的解决方案。这将消除错误消息本身。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2015-05-06
    • 2020-06-12
    • 1970-01-01
    相关资源
    最近更新 更多