Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using a custom 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.

EDIT:

This is my relevant code which cause the problem according to the breakpoint:

[tableviewSupport registerNib:[UINib nibWithNibName:@"HotelCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CustomCellCId];
like image 870
Luca Avatar asked Apr 03 '12 15:04

Luca


2 Answers

Another solution:

I had UITableViewCell, but in my XIB there was an extra object, a label which I had misplaced.

So check your XIB carefully to make sure there is only one object in the object table on the left side.

enter image description here

like image 149
Ryan Heitner Avatar answered Oct 26 '22 18:10

Ryan Heitner


I'm not quite sure what you are trying to do in your provided code. You will unload your nib in tableView:cellForRowAtIndexPath:. 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. 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. But, if one day Apple decides to stick something else in that nibArray, a for loop protects you against that.

like image 1
jmstone617 Avatar answered Oct 26 '22 18:10

jmstone617