【发布时间】:2014-06-08 19:57:06
【问题描述】:
在调用 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];
}
【问题讨论】:
-
向我展示你对 tableView:viewForHeaderInSection: 方法和 prepareForReuse 的实现:
-
谢谢@bilobatum 我已经编辑了帖子,实现了您要求的两个功能。
-
其实我想知道我的问题是否与这个问题中的类似问题有关:stackoverflow.com/questions/17581550/…。遗憾的是,那里的建议对我的情况都没有帮助。
-
经过更多测试,我发现错误肯定是由调用
insertSections:withRowAnimation:然后endUpdates引起的。并且绝对只有当标题被重用时。动画期间的布局没有明显的问题,动画完成后布局是正确的。我会相应地更新我的问题。 -
我在自定义 UIView 用作 UITableView 部分标题时遇到了类似的问题,但只在调用时抛出异常 - tableView:reloadSection:(我用它来“折叠”一部分 tableView 单元格)跨度>
标签: ios uitableview autolayout