【问题标题】:Crash with 'Could not load NIB in bundle ' while reloadRowsAtIndexPaths在 reloadRowsAtIndexPaths 时出现“无法在包中加载 NIB”导致崩溃
【发布时间】:2014-01-27 06:07:30
【问题描述】:

在我的应用程序中,当用户点击该单元格中的特定按钮时,我需要展开tableviewcell(这就像一个切换,点击将展开该行,而点击其他时间将折叠该行)。因此,我实现了自定义委托方法,以了解需要重新加载哪个单元格并使用'reloadRowsAtIndexPaths' 重新加载该特定单元格。

第一次点击按钮时它工作正常,甚至第二次点击按钮,但它在第三次时崩溃说“无法在捆绑包中加载 NIB:'NSBundle </Users/path/myApp.app> (loaded)',名称为 'ContactCell''

我的问题是重新加载第一次和第二次工作的单元格,为什么它第三次崩溃?

我的代码:

[contactTableView beginUpdates];   // Inside the custom delegate method
[contactTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexpath] withRowAnimation:UITableViewRowAnimationNone]; // indexpath is which i get from cell through custom delegate method
[contactTableView endUpdates];

 ContactTableCell *cell; //Inside the cellForRowAtIndexPath
    static NSString *CellIdentifier = @"ContactCell";

    cell =  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        // load a new cell from the nib file
        [[NSBundle mainBundle] loadNibNamed:@"ContactCell" owner:self options:nil];
        cell = self.contactCell;
        cell.delegate = self;
        cell.indexPath = indexPath;
        self.contactCell = nil;
    }

为什么会崩溃?

【问题讨论】:

  • 这个单元格的笔尖是什么? ContactCell 还是 ContactTableCell?
  • @Mani ContactCell.xib。它只是偶尔崩溃,而不是所有时候。所以,笔尖名称不是我认为的问题。
  • 你的tableview宽度不变?为什么不能增加tableview的宽度?
  • @footyapps27 增加 tableView 的宽度如何解决这个问题?
  • 变量的开头不应使用大写字母(即 CellIdentifier -> cellIdentfier)。另外我认为您不需要在此方法中加载笔尖,您需要在ContactTableCell 类中进行

标签: ios objective-c uitableview


【解决方案1】:

好的,这有几个问题。首先,从笔尖加载不应在此类中,而应在init 方法中的ContactTableCell 类中(initWithNib 或您要调用的名称)

您的 XIB 需要确保 ContactTableCell 也是分配给它的自定义类。

所以你的 nib 初始化会像这样

//in ContactTableCell.m
-(id)initWithNib{
      self = [[[NSBundle mainBundle] loadNibNamed:@"ContactCell" owner:self options:nil] firstObject];
      if(self){
         //do extra customisation stuff/ set labels etc.
      }
      return self;
 }

然后你会打电话

if (cell == nil) {
    // load a new cell from the nib file
    cell = [[ContactTableCell alloc] initWithNib];
    cell.delegate = self;
    cell.indexPath = indexPath;
}

【讨论】:

  • 嗨,我试过了,我收到了这个错误 ** 由于未捕获的异常 'NSUnknownKeyException' 导致应用程序终止,原因:'[ setValue:forUndefinedKey:]:这个类不是键值编码符合键contactCell。 **
  • contactCell 在哪里定义/初始化,它的类型是什么。您能否编辑您的帖子以包含有关contactCell 的代码?
【解决方案2】:

viewDidLoad注册您的笔尖一次。

 NSString *cellIdentifier=@"cellIdentifier";

 UINib *nib=[UINib nibWithNibName:cellIdentifier bundle:[NSBundle mainBundle]];

 [self.yourTableView registerNib:nib forCellReuseIdentifier: cellIdentifier];

希望这会奏效

【讨论】:

  • 嗨,我在 viewDidLoad 中尝试了上面的代码,但它抛出了一个错误 ** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]:此类与键contactCell 的键值编码不兼容。 **
  • 使用 IB Attribute Inspector 设置单元格标识符(标识符与 registerNib 方法中添加的标识符相同)
猜你喜欢
  • 2021-10-28
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 2012-06-03
  • 2011-07-03
相关资源
最近更新 更多