UITableView load a Custom Cell from NIB file throws NSInternalInconsistencyException

ios, nib, objective-c, uitableview

Solution

To create custom cell from NIB file try this

- (void)viewDidLoad
{
    [tableView registerNib:[UINib nibWithNibName:CellIdentifier bundle:nil] forCellReuseIdentifier:CellIdentifier];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    ......
    return cell;
}

CellIdentifier is a name of NIB file, used also as cell identifier.

Problem

In an ios application I have a UITableView. In the `cellForRowAtIndexPath` method I need to return a custom cell using it's NIB name. For that I use `loadNibNamed`. (I will fill the data in the cell after the load in the 'willDisplayCellforRowAtIndexPath') MyItemCell is a XIB file (MyItemCell.xib) that containg 2 UIImageView and a UIButton (Each item has a tag) This is my code: In my viewController ``` - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return [ViewHelper loadCustomCellWithNibName:@"MyItemCell" owner:self]; } ``` And the method to load the Custom cell from NIB ``` + (UITableViewCell *) loadCustomCellFromNib:(NSString *)nibName owner:(id)owner { UITableViewCell *cell = nil; NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:nibName owner:owner options:nil]; if([nibObjects count] > 0 ) { cell = [nibObjects objectAtIndex:0]; } else { NSLog(@"Failed to load %@ XIB file!", nibName); } return cell; } ``` Everything works correctly in all the testings. However I received a crash from some users that I was unable to reproduce. This is the crash: ``` NSInternalInconsistencyException Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/7A24cE79-131F-523F-4C00-23B523ARG123/MyApp.app> (loaded)' with name 'MyItemCell' ``` The stack trace: ``` 0 CoreFoundation 0x39b432a3 __exceptionPreprocess + 163 + 162 1 libobjc.A.dylib 0x33a3297f objc_exception_throw + 31 + 30 2 CoreFoundation 0x39b431c5 -[NSException initWithCoder:] + 1 3 UIKit 0x32e12491 -[UINib instantiateWithOwner:options:] + 1637 + 1636 4 UIKit 0x32e1a1d7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 139 + 138 5 MyApp 0x00047ded +[ViewHelper loadCustomCellFromNib:owner:] (ViewHelper.m:349) 6 MyApp 0x00034003 -[BuildViewController tableView:cellForRowAtIndexPath:] (BuildViewController.m:2432) 7 UIKit 0x32cc0545 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 413 + 412 8 UIKit 0x32ca530b -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1311 + 1310 9 UIKit 0x32cbc7c7 -[UITableView layoutSubviews] + 207 + 206 10 UIKit 0x32c78803 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 259 + 258 ``` The problem is that I was not able to reproduce this crash. Any idea on what might have caused the crash? Or any solutions to avoid such an error? Thanks a lot for any help EDIT: Just to clarify more, This is working perfectly fine on any testing that I'm doing. This crash appeared only 1 time for 1 user so the problem is not with the code. I am just searching for reasons that might cause this crash in a very specific scenario. Thanks

Original source