UITableView loads custom cell from NIB file throwing NSInternalInconsistencyException
In ios app I have a UITableView. In the method, cellForRowAtIndexPath
I need to return a custom cell using its NIB name. For this I use loadNibNamed
. (I will fill in the data in the cell after loading in 'willDisplayCellforRowAtIndexPath')
MyItemCell is a XIB file (MyItemCell.xib) that contains 2 UIImageView and 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 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 tests. However, I got crash from some users that I was unable to reproduce.
This is an accident:
NSInternalInconsistencyException
Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/7A24cE79-131F-523F-4C00-23B523ARG123/MyApp.app> (loaded)' with name 'MyItemCell'
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 have not been able to reproduce this crash.
Any idea what could have caused the crash? Or any solutions to avoid such a mistake?
Thanks a lot for any help
EDIT:
Just to clarify, this works great on any testing I do. This glitch only appeared once for 1 user, so the problem is not in the code. I'm just looking for the reasons that might cause this to fail in a very specific scenario. Thanks to
source to share
To create a custom cell from a NIB file try
- (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 the name of the NIB file, also used as the cell identifier.
source to share
Please try this ... It won't give any problem ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"albumListCell";
albumListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = (albumListCell *) [[[NSBundle mainBundle] loadNibNamed:@"albumListCell" owner:self options:nil] lastObject];
}
return cell;
}
It is not possible to create a tableViewCell with a nib file .. Create a simple View controller and then change the UIViewController to UITableViewCell ...
source to share