iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UITableView从NIB文件加载自定义单元格抛出NSInternalInconsistencyException大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在ios应用程序中,我有一个UITableView.在cellForRowATindexPath方法中,我需要使用它的NIB名称返回自定义单元格.为此,我使用loadNibNamed. (我将在’willDisplayCellforRowATindexPath’中加载后填充单元格中的数据)

@H_497_5@myItemCell是一个XIB文件(MyItemCell.xib),包含2个UIImageView和一个UIButton(每个项目都有一个标签)

这是我的代码

在我的viewController中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath
{
    return [ViewHelper loadCustomcatellWithNibName:@"MyItemCell" owner:self];
}

以及从NIB加载Custom单元格的方法

+ (UITableViewCell *) loadCustomcatellFromNib:(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;
}

在所有测试中一切正常.但是我收到了一些我无法重现的用户崩溃.

这是崩溃:

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 loadCustomcatellFromNib: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

问题是我无法重现这次崩溃.

什么可能导致崩溃?或者任何避免这种错误解决方案?

非常感谢您的帮助

编辑:

只是为了澄清更多,这对我正在进行的任何测试都非常好.对于1个用户,这次崩溃只出现了一次,所以问题不在于代码.我只是在非常具体的情况下搜索可能导致崩溃的原因.谢谢

解决方法

要从NIB文件创建自定义单元格,请尝试此操作

- (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文件名称,也用作单元标识符.

大佬总结

以上是大佬教程为你收集整理的ios – UITableView从NIB文件加载自定义单元格抛出NSInternalInconsistencyException全部内容,希望文章能够帮你解决ios – UITableView从NIB文件加载自定义单元格抛出NSInternalInconsistencyException所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。