Crash when using auto layout on UITableViewCell with backgroundView

Originator:lindberg+openradar
Number:rdar://12519234 Date Originated:10/17/2012
Status:Duplicate/11690549 Resolved:iOS7
Product:iPad/iPhone SDK Product Version:6.0
Classification:Crash Reproducible:Yes
 
When using a UITableViewCell which has a backgroundView set, it crashes when trying to use auto layout in iOS 6.  It crashes on both iPhone and iPad simulators.


2012-10-17 13:27:32.387 TestAutoLayout[70680:c07] *** Assertion failure in -[UITableViewCell layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2372/UIView.m:5776
2012-10-17 13:27:32.389 TestAutoLayout[70680:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableViewCell's implementation of -layoutSubviews needs to call super.'


It appears that UITableViewCell's -layoutSubviews method does not call super.  The following category (which can be enabled by changing the "#if 0" to "#if 1" in AppDelegate.m) appears to fix the problem.


@implementation UITableViewCell (FixUITableViewCellAutolayoutIHope)

+ (void)load
{
    Method existing = class_getInstanceMethod(self, @selector(layoutSubviews));
    Method new = class_getInstanceMethod(self, @selector(_autolayout_replacementLayoutSubviews));
    
    method_exchangeImplementations(existing, new);
}

- (void)_autolayout_replacementLayoutSubviews
{
    [super layoutSubviews];
    [self _autolayout_replacementLayoutSubviews]; // not recursive due to method swizzling
    [super layoutSubviews];
}

@end

Comments

It appears this bug was fixed in iOS7.

By lindberg+openradar at Oct. 18, 2013, 12:46 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!