我正在尝试将自动布局合并到我的UITableViewHeaderFooterView子类中。该类非常基础,只有两个标签。这是完整的子类:
@implementation MBTableDetailStyleFooterView
static void MBTableDetailStyleFooterViewCommonSetup(MBTableDetailStyleFooterView *_self) {
UILabel *rightLabel = [[UILabel alloc] init];
_self.rightLabel = rightLabel;
rightLabel.translatesAutoresizingMaskIntoConstraints = NO;
[_self.contentView addSubview:rightLabel];
UILabel *leftLabel = [[UILabel alloc] init];
_self.leftLabel = leftLabel;
leftLabel.translatesAutoresizingMaskIntoConstraints = NO;
[_self.contentView addSubview:leftLabel];
NSDictionary *views = NSDictionaryOfVariableBindings(rightLabel, leftLabel);
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[leftLabel]-(>=10)-[rightLabel]-10-|" options:0 metrics:nil views:views];
[_self.contentView addConstraints:horizontalConstraints];
// center views vertically in super view
NSLayoutConstraint *leftCenterYConstraint = [NSLayoutConstraint constraintWithItem:leftLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_self.contentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
[_self.contentView addConstraint:leftCenterYConstraint];
NSLayoutConstraint *rightCenterYConstraint = [NSLayoutConstraint constraintWithItem:rightLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_self.contentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
[_self.contentView addConstraint:rightCenterYConstraint];
// same height for both labels
NSLayoutConstraint *sameHeightConstraint = [NSLayoutConstraint constraintWithItem:leftLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:rightLabel attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
[_self.contentView addConstraint:sameHeightConstraint];
}
+ (BOOL)requiresConstraintBasedLayout {
return YES;
}
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithReuseIdentifier:reuseIdentifier];
MBTableDetailStyleFooterViewCommonSetup(self);
return self;
}
@end
此类在带有2个部分的tableView的第一部分中用作页脚。第一部分包含动态项目。第二部分只有一行,用于向第一部分添加新项目。
如果在第一部分中没有任何项目,我将隐藏footerView。因此,当我添加第一个新项目时,我必须重新加载该部分,以便出现footerView。完成所有这些操作的代码如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 1) {
BOOL sectionNeedsReload = ([self.data count] == 0); // reload section when no data (and therefor no footer) was present before the add
[self.data addObject:[NSDate date]];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[self.data count]-1 inSection:0];
if (sectionNeedsReload) {
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else {
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
[self configureFooter:(MBTableDetailStyleFooterView *)[tableView footerViewForSection:0] forSection:0];
}
}
- (void)configureFooter:(MBTableDetailStyleFooterView *)footer forSection:(NSInteger)section {
footer.leftLabel.text = @"Total";
footer.rightLabel.text = [NSString stringWithFormat:@"%d", [self.data count]];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
MBTableDetailStyleFooterView *footer = nil;
if (section == 0 && [self.data count]) {
footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"Footer"];
[self configureFooter:footer forSection:section];
}
return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
CGFloat height = 0;
if (section == 0 && [self.data count]) {
height = 20.0f;
}
return height;
}
没什么好看的。但是,一旦在我的tableView上调用
reloadSections:withRowAnimations:
,就会引发异常,因为它“无法同时满足约束”。 tableView的某个位置向我的页脚添加了转换后的自动调整大小的蒙版约束。
(
"<NSLayoutConstraint:0x718a1f0 H:[UILabel:0x7189130]-(10)-| (Names: '|':_UITableViewHeaderFooterContentView:0x7188df0 )>",
"<NSLayoutConstraint:0x7189e30 H:[UILabel:0x71892c0]-(>=10)-[UILabel:0x7189130]>",
"<NSLayoutConstraint:0x718a0a0 H:|-(10)-[UILabel:0x71892c0] (Names: '|':_UITableViewHeaderFooterContentView:0x7188df0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x7591ab0 h=--& v=--& H:[_UITableViewHeaderFooterContentView:0x7188df0(0)]>"
)
当我用对
reloadSections:withRowAnimations:
的调用替换reloadData
时,没有添加自动调整大小的掩码约束,并且一切正常。有趣的是,异常告诉我它试图打破约束
<NSLayoutConstraint:0x7189e30 H:[UILabel:0x71892c0]-(>=10)-[UILabel:0x7189130]>
但是,当我在随后的
configureFooter:forSection:
调用中记录约束时,该约束仍然存在,但是自动调整大小的掩码约束消失了约束正是我设置的约束。
(
"<NSLayoutConstraint:0x718a0a0 H:|-(10)-[UILabel:0x71892c0] (Names: '|':_UITableViewHeaderFooterContentView:0x7188df0 )>",
"<NSLayoutConstraint:0x7189e30 H:[UILabel:0x71892c0]-(>=10)-[UILabel:0x7189130]>",
"<NSLayoutConstraint:0x718a1f0 H:[UILabel:0x7189130]-(10)-| (Names: '|':_UITableViewHeaderFooterContentView:0x7188df0 )>",
"<NSLayoutConstraint:0x718a3f0 UILabel:0x71892c0.centerY == _UITableViewHeaderFooterContentView:0x7188df0.centerY>",
"<NSLayoutConstraint:0x718a430 UILabel:0x7189130.centerY == _UITableViewHeaderFooterContentView:0x7188df0.centerY>",
"<NSLayoutConstraint:0x718a4b0 UILabel:0x71892c0.height == UILabel:0x7189130.height>"
)
这种自动调整大小的 mask 约束来自何处?去哪儿了?
我想念什么吗?我第一次看自动布局就像一个星期前,所以这是完全有可能的。
最佳答案
从iOS 9开始的工作解决方案
在您的UITableViewHeaderFooterView子类中,放置以下代码。
- (void)setFrame:(CGRect)frame {
if (frame.size.width == 0) {
return;
}
[super setFrame:frame];
}
说明:tableview处理标题 View 的布局,并且通过手动操作框架来实现(即使启用了自动布局,也可以)。
如果检查页眉/页脚 View 上的宽度约束,则有两个,一个包含在 super View (表 View )中以获取宽度,另一个包含在页眉/页脚 View 本身中以获取宽度。
super View 上包含的约束是NSAutoresizingMaskLayoutConstraint,它是tableview依赖于框架来操作标题的赠品。将标题 View 上的translatesAutoresizingMaskIntoConstraints切换为NO会有效地破坏其外观,这是另一个放弃。
似乎在某些情况下,这些页眉/页脚 View 的框架将更改为零宽度,对我而言,这是插入行并重新使用页眉 View 时的情况。我的猜测是,即使您不使用动画,也要通过在UITableView代码中的某个位置通过以零宽度开始帧来为动画做准备。
该解决方案应能很好地工作,并且不会影响滚动性能。
关于ios - 具有自动布局和节重新加载的UITableViewHeaderFooterView子类不能很好地协同工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17581550/