具有自动布局和部分重新加载的UITableViewHeaderFooterView子类将无法很好地协同工作

Mat*_*uch 20 cocoa-touch uitableview ios autolayout

我正在尝试将自动布局合并到我的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
Run Code Online (Sandbox Code Playgroud)

此类在tableView的第一部分中用作页脚,包含2个部分.第一部分包含动态项目.第二部分只有一行,用于向第一部分添加新项目.

如果第一部分中没有项目,则隐藏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;
}
Run Code Online (Sandbox Code Playgroud)

没什么好看的.但是,只要reloadSections:withRowAnimations:在我的tableView上调用它就会抛出异常,因为它"无法同时满足约束".

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)]>"
)
Run Code Online (Sandbox Code Playgroud)

当我替换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>"
)
Run Code Online (Sandbox Code Playgroud)

这个自动调整大小掩码约束来自哪里?它去哪儿了?

我错过了什么吗?我第一次看到汽车布局就像一周前,所以这是完全可能的.

Jos*_*eld 24

适用于iOS 9的工作解决方案

在您的UITableViewHeaderFooterView子类中放置以下代码.

- (void)setFrame:(CGRect)frame {
    if (frame.size.width == 0) {
        return;
    }

    [super setFrame:frame];
}
Run Code Online (Sandbox Code Playgroud)

说明:

tableview处理标题视图的布局,它通过手动操作框架来实现(即使启用了autolayout也是如此).

如果检查页眉/页脚视图上的宽度约束,则有两个,一个包含在超视图(表视图)中,宽度为一个,页眉/页脚视图中包含一个宽度.

超级视图中包含的约束是NSAutoresizingMaskLayoutConstraint,它是tableview依赖于操作标题的帧的赠品.在标题视图上将translatesAutoresizingMaskIntoConstraints切换为NO会有效地破坏其外观,这是另一种消除.

看起来在某些情况下,这些页眉/页脚视图的帧会更改为宽度为零,对我而言,插入行并重新使用标题视图时.我的猜测是,在UITableView代码的某处,即使您没有使用动画,也可以通过以零宽度启动帧来制作动画.

此解决方案应该运行良好,不应影响滚动性能.

  • 有用!Swift 3+版本:覆盖var frame:CGRect {get {return super.frame} set {如果newValue.width == 0 {return} super.frame = newValue}} (3认同)
  • 非常荒谬,但这对我来说非常有效!谢谢你的提示! (2认同)

Ben*_*ohn 7

我上周跑进去了.

我消除警告的方式是将我所需的约束更改为优先级为999.这是一个解决方法,而不是一个修复,但它确实绕过布局期间抛出,捕获和记录的异常.

对我不起作用的事情.

建议是设定estimatedRowHeight.我试图设置estimatedSectionHeaderHeight,但这没有帮助.estimatedSectionFooterHeight在我不想要的地方设置一个创建的空页脚,这有点奇怪.

我还尝试设置translatesAutoresizingMaskIntoConstraints = NO;页眉页脚视图,并在其内容视图上.既没有摆脱警告,也没有导致布局彻底破坏.

  • 这对我有用:`customConstraints.forEach {$ 0.priority = 999}`:) (2认同)

小智 0

我没有看到您在页脚本身上调用的translatesAutoresizingMaskIntoConstraints = NO 的位置​​。创建时应该这样做吗?

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    self.translatesAutoresizingMaskIntoConstraints = NO;
    MBTableDetailStyleFooterViewCommonSetup(self);
    return self;
}
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这不起作用,因为 tableView 显然不会调用“[superlayoutSubviews]”。`由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'执行-layoutSubviews后仍然需要自动布局。UITableView的-layoutSubviews的实现需要调用super.'`。 (3认同)