Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView从NIB文件加载自定义单元格会引发NSInternalInconsistencyException_Ios_Objective C_Uitableview_Nib - Fatal编程技术网

Ios UITableView从NIB文件加载自定义单元格会引发NSInternalInconsistencyException

Ios UITableView从NIB文件加载自定义单元格会引发NSInternalInconsistencyException,ios,objective-c,uitableview,nib,Ios,Objective C,Uitableview,Nib,在ios应用程序中,我有一个UITableView。在cellforrowatinexpath方法中,我需要使用NIB名称返回一个自定义单元格。为此,我使用了loadNibNamed。(我将在“willDisplayCellforRowAtIndexPath”中加载后在单元格中填充数据) MyItemCell是一个XIB文件(MyItemCell.XIB),其中包含2个UIImageView和一个UIButton(每个项目都有一个标记) 这是我的代码: 在我的viewController中 -

在ios应用程序中,我有一个UITableView。在
cellforrowatinexpath
方法中,我需要使用NIB名称返回一个自定义单元格。为此,我使用了
loadNibNamed
。(我将在“willDisplayCellforRowAtIndexPath”中加载后在单元格中填充数据)

MyItemCell是一个XIB文件(MyItemCell.XIB),其中包含2个UIImageView和一个UIButton(每个项目都有一个标记)

这是我的代码:

在我的viewController中

- (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'
问题是我无法重现这次崩溃

你知道是什么导致了这次车祸吗?或者有什么办法来避免这种错误

非常感谢你的帮助

编辑:


只是为了进一步澄清,这在我正在进行的任何测试中都非常有效。此崩溃仅对1个用户出现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…

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

需要记住的重要一点是,nibname和单元标识符是不同的。名称在标识检查器中,标识符在属性检查器中
- (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;
}