UITableView从NIB文件加载自定义单元格会引发NSInternalInconsistencyException [英] UITableView load a Custom Cell from NIB file throws NSInternalInconsistencyException
问题描述
在ios应用程序中,我有一个UITableView.在cellForRowAtIndexPath
方法中,我需要使用其NIB名称返回一个自定义单元格.为此,我使用loadNibNamed
. (加载"willDisplayCellforRowAtIndexPath"后,我将在单元格中填充数据)
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是一个XIB文件(MyItemCell.xib),其中包含2个UIImageView和一个UIButton(每个项都有一个标记)
MyItemCell is a XIB file (MyItemCell.xib) that containg 2 UIImageView and a UIButton (Each item has a tag)
这是我的代码:
在我的viewController中
In my viewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [ViewHelper loadCustomCellWithNibName:@"MyItemCell" owner:self];
}
以及从NIB加载自定义单元格的方法
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.
这是崩溃:
NSInternalInconsistencyException
Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/7A24cE79-131F-523F-4C00-23B523ARG123/MyApp.app> (loaded)' with name 'MyItemCell'
堆栈跟踪:
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?
非常感谢您的帮助
只需澄清一下,这在我正在进行的任何测试中都可以正常工作.此崩溃仅对1个用户出现1次,因此问题与代码无关.我只是在寻找可能在特定情况下导致此崩溃的原因.谢谢
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
推荐答案
要从NIB文件创建自定义单元格,请尝试
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是NIB文件的名称,也用作单元标识符.
CellIdentifier is a name of NIB file, used also as cell identifier.
这篇关于UITableView从NIB文件加载自定义单元格会引发NSInternalInconsistencyException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!