重用UITableViewHeaderFooterView时出现NSAutoresizingMaskLayoutConstraint错误

时间:2014-04-23 19:30:20

标签: ios uitableview autolayout

致电insertSections:withRowAnimation:endUpdates后,我收到以下错误消息。该错误涉及自定义UITableViewHeaderFooterView中的自动布局,但仅限于dequeueReusableHeaderFooterViewWithIdentifier:重复使用标头时。第一次它工作正常,没有错误。

Unable to simultaneously satisfy constraints.
...
(
    "<NSLayoutConstraint:0x10b77f9f0 V:|-(8)-[UIView:0x10b77d0f0]   (Names: '|':WYBDetailHeaderView:0x10b77e620 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x10b77cac0 h=--& v=--& V:[WYBDetailHeaderView:0x10b77e620(0)]>",
    "<NSLayoutConstraint:0x10b77fa40 V:[UIView:0x10b77d0f0]-(>=4)-|   (Names: '|':WYBDetailHeaderView:0x10b77e620 )>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x10b77fa40 V:[UIView:0x10b77d0f0]-(>=4)-|   (Names: '|':WYBDetailHeaderView:0x10b77e620 )>

水平约束存在类似的错误。问题是宽度和高度都显示NSAutoresizingMaskLayoutConstraint设置为零。但是,一旦动画完成,标题布局是正确的,看起来很好。

我已经按照这个相关问题的建议而没有运气:UITableViewHeaderFooterView subclass with auto layout and section reloading won't work well together

有没有人遇到过类似的事情?

我能做些什么来避免警告吗?

这是我UITableView中的实现:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WYBDetailHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WYB"];
    [header setupWithTitle:@"Title" subtitle:@"Subtitle"];
    return header;
}

WYBDetailHeaderView的实现(它使用我在registerNib:forHeaderFooterViewReuseIdentifier:注册的NIB):

- (void)setupWithTitle:(NSString *)title subtitle:(NSString *)subtitle
{
    // Set labels.
    self.titleLabel.text = title.uppercaseString;
    self.subtitleLabel.text = subtitle;

    // Clear image.
    self.headerImageView.image = nil;
    self.emptyImage = NO;

    // Mark for layout update.
    [self setNeedsUpdateConstraints];
    [self setNeedsLayout];
}

- (void)prepareForReuse
{
    self.prototype = NO;
    self.emptyImage = NO;
}

- (void)updateConstraints
{
    // See if there is an image.
    if (self.emptyImage || self.headerImageView.image) {
        self.imageWidthConstraint.constant = 20;
        self.imageHeightConstraint.constant = 20;
        self.imageTitleSpaceConstraint.constant = 8;
    }
    else {
        self.imageWidthConstraint.constant = 0;
        self.imageHeightConstraint.constant = 0;
        self.imageTitleSpaceConstraint.constant = 0;
    }

    [super updateConstraints];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.titleLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.titleLabel.frame);
    self.subtitleLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.subtitleLabel.frame);
    [super layoutSubviews];
}

1 个答案:

答案 0 :(得分:0)

我遇到了类似的错误,并使用了estimatedHeightForRowAtIndexPath导致它。