1
votes

My code crashes when I added a subview to a table view, using the AutoLayout technique, and here is my code:

@interface GNViewController () {
    UITableView     *_tvResults;
    UIView          *_view;
}

@end

@implementation GNViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blueColor];

_tvResults = [UITableView new];
_tvResults.backgroundColor = [UIColor greenColor];
[self.view addSubview:_tvResults];

_view = [UIView new];
_view.backgroundColor = [UIColor redColor];
[_tvResults addSubview:_view];

_tvResults.translatesAutoresizingMaskIntoConstraints = NO;
_view.translatesAutoresizingMaskIntoConstraints = NO;

NSArray *cs = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_tvResults]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tvResults)];
[self.view addConstraints:cs];

cs = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_tvResults]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tvResults)];
[self.view addConstraints:cs];



cs = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_view]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view)];
[_tvResults addConstraints:cs];

cs = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_view]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view)];
[_tvResults addConstraints:cs];
}

@end

The crash message reads:

2014-03-14 22:45:06.801 asdjasjdasdasd[3758:a0b] * Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2903.2/UIView.m:8536 2014-03-14 22:45:06.805 asdjasjdasdasd[3758:a0b] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'

Anyone please help me, thanks in advance!

1

1 Answers

3
votes

The problem is that you are directly adding a subview to a UITableView and trying to involve it in constraints of the table view. Those are both bad ideas, and the second one in particular is causing your crash. Table views are not set up for that sort of thing.

It would help if you would explain what you are actually trying to do. If you want to lay something on top of the table view so that it covers it (though why you would want to do that beats the heck out of me), make it another subview of self.view and pin it with constraints to self.view. Leave the table view to fend for itself.

EDIT: From a comment, I gather that this worked when you did it without auto layout. Then keep doing it without auto layout! The problem stems from your attempt to impose auto layout internally on a table view, which doesn't use auto layout and doesn't like that. There is no problem with having the table view itself be resized/positioned by auto layout but not use auto layout on its subview. Just go back to the way you were doing this before and it will work as it did.