简体   繁体   English

为标识符注册的无效nib((null)) - nib必须只包含一个顶级

[英]Invalid nib registered for identifier ((null)) - nib must contain exactly one top level

I'm using a custom UITableViewCell . 我正在使用自定义UITableViewCell When I try to run my code, I got this exception stack and I can't understand the source of the problem: 当我尝试运行我的代码时,我得到了这个异常堆栈,我无法理解问题的根源:

terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier ((null)) - nib must contain exactly one top level object which must be a UITableViewCell instance'

Please note I am not using Storyboard. 请注意我没有使用Storyboard。

EDIT: 编辑:

This is my relevant code which cause the problem according to the breakpoint: 这是我根据断点导致问题的相关代码:

[tableviewSupport registerNib:[UINib nibWithNibName:@"HotelCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CustomCellCId];

Another solution: 另一种方案:

I had UITableViewCell, but in my XIB there was an extra object, a label which I had misplaced. 我有UITableViewCell,但在我的XIB中有一个额外的对象,一个我错放的标签。

So check your XIB carefully to make sure there is only one object in the object table on the left side. 因此,请仔细检查您的XIB,以确保左侧的对象表中只有一个对象。

在此输入图像描述

I'm not quite sure what you are trying to do in your provided code. 我不太确定你在提供的代码中尝试做什么。 You will unload your nib in tableView:cellForRowAtIndexPath:. 您将在tableView:cellForRowAtIndexPath:中卸载您的nib。 You can use the below code: 您可以使用以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Assuming you set a reuse identifier "cellId" in the nib for your table view cell...
    HotelCell *cell = (HotelCell *)[tableView dequeueReusableCellWithIdentifier:@"cellId"];
    if (!cell) {
        // If you didn't get a valid cell reference back, unload a cell from the nib
        NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"HotelCell" owner:nil options:nil];
        for (id obj in nibArray) {
            if ([obj isMemberOfClass:[HotelCell class]]) {
                // Assign cell to obj
                cell = (HotelCell *)obj;
                break;
            }
        }
     }

     return cell;
}

Now, based on the error you posted, iOS is expecting a single object in the array, so the for loop may be unnecessary. 现在,根据您发布的错误,iOS期望数组中有一个对象,因此for循环可能是不必要的。 I've done this by checking if [nibArray count] == 1 and then grabbing the objectAtIndex:0 and assigning it to cell instead of looping, and this works as well. 我通过检查[nibArray count] == 1然后抓取objectAtIndex:0并将其分配给单元而不是循环来完成此操作,这也是有效的。 But, if one day Apple decides to stick something else in that nibArray, a for loop protects you against that. 但是,如果有一天Apple决定在nibArray中粘贴其他东西,那么for循环可以保护你免受攻击。

This may not be the answer to the OP's question, but I had the same error with a different solution, so I'll answer this question for the sake of Googlers who come hereafter. 这可能不是OP的问题的答案,但我有一个不同的解决方案同样的错误,所以我会回答这个问题,为了后来的Google员工。

I did have a single UITableViewCell (actually, my own subclass) in my XIB -- so I couldn't understand why it wasn't working. 我在我的XIB中确实有一个UITableViewCell (实际上是我自己的子类) - 所以我无法理解为什么它不起作用。

The key seems to be: 关键似乎是:

When you've created a XIB file with a single UITableViewCell or subclass (eg if you drag on a UITableViewCell from the library on the lower right), Interface Builder shows you an option under the "Table View Cell" section of the Inspector tab. 当您使用单个UITableViewCell或子类创建XIB文件时(例如,如果您从右下方的库中拖动UITableViewCell),Interface Builder会在Inspector选项卡的“Table View Cell”部分下显示一个选项。

I usually used to create a UIView and changing its type to my UITableViewCell subclass, so I had never noticed that option. 我通常用来创建一个UIView并将其类型更改为我的UITableViewCell子类,所以我从来没有注意到这个选项。

It is there that you need to type the identifier (as well as in your registerNib:withReuseIdentifer: call). 正是在那里 ,你需要输入识别码(以及在registerNib:withReuseIdentifer:调用)。

More frustrating for me when experiencing this was that it didn't crash EVERY time, only the first time. 在遇到这种情况时,对我来说更令人沮丧的是它每次都没有崩溃,只是第一次。 Weird. 奇怪的。

在我的例子中,我在tableviewcell中设置了一个具有名称类的属性(例如:@property(强,非原子)IBOutlet UILabel *类),这给出了上面提到的错误,然后名称“class”不是客观c类型,更改(class = classabc)解决上述问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为标识符注册的无效nib(CELLNAME) - nib必须包含一个必须是UITableViewCell实例的顶级对象 - invalid nib registered for identifier (CELLNAME) - nib must contain exactly one top level object which must be a UITableViewCell instance Nib 必须恰好包含一个顶级 object,它必须是 UITableViewCell 实例。 适用于其他 UITableViews - Nib must contain exactly one top level object which must be a UITableViewCell instance. Works in other UITableViews 我收到一个错误 - nib 必须恰好包含一个顶级 object,它必须是一个 UITableViewCell 实例'” - I am getting an error - nib must contain exactly one top level object which must be a UITableViewCell instance'" nib必须只包含一个顶级对象,该对象必须是UITableViewHeaderFooterView实例 - nib must contain exactly one top level object which must be a UITableViewHeaderFooterView instance Nib 必须恰好包含一个顶级对象,该对象必须是 UITableViewCell 实例 - Nib must contain exactly one top level object which must be a UITableViewCell instance Xcode 8:将UISearchBarController添加到UITableViewHeaderFooterView崩溃:笔尖必须恰好包含一个顶级对象 - Xcode 8: Adding UISearchBarController to UITableViewHeaderFooterView crash: nib must contain exactly one top level object 无效的笔尖注册为标识符 - Invalid nib registered for identifier 为标识符注册的笔尖无效(单元格) - invalid nib registered for identifier (Cell) 我无法解决“为标识符注册的无效笔尖(BNRItemCell)” - I can't solve “invalid nib registered for identifier (BNRItemCell)” 必须为该标识符注册一个笔尖或一个类或连接一个原型单元 - Must register a nib or a class for the identifier or connect a prototype cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM