UITableView 从 NIB 文件加载自定义单元格时抛出 NSInternalInconsistencyException。

3
在iOS应用程序中,我有一个UITableView。在“cellForRowAtIndexPath”方法中,我需要使用它的NIB名称返回自定义单元格。为此,我使用“loadNibNamed”。(在“willDisplayCellforRowAtIndexPath”中加载后,我将填充单元格中的数据)
MyItemCell是一个XIB文件(MyItemCell.xib),其中包含2个UIImageView和一个UIButton(每个项目都有一个标记)
这是我的代码:
在我的视图控制器中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [ViewHelper loadCustomCellWithNibName:@"MyItemCell" owner:self];
}

加载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;
}

在所有的测试中,一切都正常工作。但是我收到了一些用户发来的崩溃报告,而我却无法复现。

以下是崩溃信息:

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 

问题是我无法重现这个崩溃。
有任何想法是什么导致了崩溃?或者有任何解决方案来避免这样的错误?
非常感谢您的帮助。
编辑:仅澄清,这在我进行的任何测试中都完美地工作。这个崩溃只出现了一次,对于一个用户,因此问题不在代码上。我只是在寻找可能在非常特定的情况下导致此崩溃的原因。谢谢。

记住重要的是,nibName 和 cell identifier 是不同的。名称在“identity inspector”中,而标识符在“attributes inspector”中。 - noobsmcgoobs
2个回答

12

要从 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文件的名称,也用作单元格标识符。


1
请尝试这个...它不会出现任何问题...
- (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;
}

创建带有nib文件的tableViewCell是不可能的。因此,需要创建一个简单的视图控制器,然后将UIViewController更改为UITableViewCell...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接