Custom UITableViewHeaderFooterView with XIB and AutoLayout

I get this error from my custom UITableViewHeaderFooterView:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. WYBDetailHeaderView implementation of -layoutSubviews needs to call super.'

I have subclassed UITableViewHeaderFooterViewand created an XIB file that I upload using registerNib:forHeaderFooterViewReuseIdentifier:to mine UITableView.

Since there is no IB UITableViewHeaderFooterView, I followed advice in other posts and simply used UIView( UITableViewHeaderFooterView in InterfaceBuilder ; UITableViewHeaderFooterView with IB ).

My custom one UITableViewHeaderFooterViewuses AutoLayout and includes two multi-line ones UILabel, and I assume that the problem is that these subtasks are on the actual UITableViewHeaderFooterView, and not .contentView. However, I do not know how to do this with IB.

This is my implementation layoutSubviews(I tried several combinations without success). I even get an error when I completely remove this function:

- (void)layoutSubviews
{
    [super layoutSubviews];
    // Commented out because of infinite loop.
    //  If this was a custom UITableViewCell I would call these methods on self.contentView
    //[self setNeedsLayout];
    //[self layoutIfNeeded];

    self.titleLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.titleLabel.frame);
    self.subtitleLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.subtitleLabel.frame);
}

How should I do it?

+4
source share
1 answer

Try this method:

-(UIView*) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

Then load the xib file into it.

0
source

Source: https://habr.com/ru/post/1536814/


All Articles