1
votes

"invalid nib registered for identifier (myGoal) - nib must contain exactly one top level object which must be a UITableViewCell instance"

Thats the exception I keep on getting. I am using both, storyboard and XIB.

In my storyboard, I have a tableview inside of a navigation view. The tableview is set to dynamic prototypes and the number of prototype cells is zero. The reason is because I wanted to add a custom cell programmatically.

The first time I added the cells programmatically, I was using one of the generic cell styles. At it would work!

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"myGoal"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myGoal"
                                                        forIndexPath:indexPath];
    //do stuff with cell
}

It would let me have a tableview with the default cell style, which has one label.

Next I wanted to customize my cell so that it would have multiple label and a few extra buttons. Normally, I'd do it in a storyboard interface builder, but this time I need to do it programatically.

So I created a custom tableviewcell class along with its Xib counterpart. In the Xib I designed it how I wanted. The problem is in getting this cell to show up in the tableview.

Does it matter that the tableview is in a storyboard and the tableviewcell is in a xib?

In order to register the custom nib, I did almost the same thing as above, yet it crashes:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UINib *dhCustomNib = [UINib nibWithNibName:@"DHTableViewCell" bundle:nil];
    [self.tableView registerNib:dhCustomNib forCellReuseIdentifier:@"myGoal"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myGoal"
                                                        forIndexPath:indexPath]; //It crashes at this point
    //do stuff with cell
}

What am I missing?

1
Can you show how your nib is set up? You need to ensure that there is one top level object and it needs to have it's class set to be a subclass of UITableViewCell - Paul.s
Your DHTableViewCell class should be a subclass of UITableViewCell and must be the only top level object in the nib - Paul.s
If you have any gesture recognisers set up the nib , they will cause this error. - Warren Burton
@Paul.s Files owner is set to DHTableViewCell. How do I set it so that there is only one top level object? - DerrickHo328
@Paul.s DHTableViewCell is already set as a subclass to uitableviewcell. In the xib's identity inspector, the custom class is already set to DHTableViewCell. I'm not sure what else I am missing. - DerrickHo328

1 Answers

0
votes

You need to set the top level object as the DHTableViewCell subclass not the file's owner.

enter image description here