0

I'm trying to create a table view with some custom cells, but I have a problem. Everything is setup correctly and when I use this UITableVC as the initial VC it all works. But when I try to add it as a child VC to another VC, I get this error:

Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460
2013-02-05 18:37:25.704 SalesBot[16284:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Statement Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

I'm not changing anything else, I'm just moving the arrow in the storyboard to another VC to make it initial.

Here is how I'm adding the UITableVC subclass as a child to another VC:

self.statementTableViewController = [[SBStatementTableViewController alloc] init];
[self.view addSubview:self.statementTableViewController.view];
[self.statementTableViewController.view setFrame:self.contentFrame];

And here is how I dequeue a cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Statement Cell";
    SBStatementCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

I understand the above code is iOS6 only, I will worry about iOS 5 later :)

I'm guessing that the cell identifiers are somehow not "global" and the tableVC can't see them when it's a child to another VC?

Please help and let me know if you need to see some more code!

Nikolay Dyankov
  • 6,491
  • 11
  • 58
  • 79
  • Can you post the code where you initialize the cell? It should look something like UITableViewCell *cell = [tableview deque...etc – Max Feb 05 '13 at 16:48

1 Answers1

0

Even though you have yet to follow up with your code, I'm willing to bet this is your issue.

Taken from: Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

You're using the dequeueReusableCellWithIdentifier:forIndexPath: method. The documentation for that method says this:

Important: You must register a class or nib file using the 
registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier:
method before calling this method.

You didn't register a nib or a class for the reuse identifier "Cell".

Looking at your code, you seem to expect the dequeue method to return nil if it doesn't have a cell to give you. You need to use the dequeueReusableCellWithIdentifier: for that behavior:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Notice that dequeueReusableCellWithIdentifier: and dequeueReusableCellWithIdentifier:forIndexPath: are different methods.

Community
  • 1
  • 1
Max
  • 5,799
  • 3
  • 20
  • 27
  • I have noticed that, dequeueReusableCellWithIdentifier:forIndexPath: is iOS 6+ only. I will try to make the suggested changes and see what happens. In the meantime, why am I not getting this error when the table VC is not a child of another VC? (when it's the initial VC) – Nikolay Dyankov Feb 05 '13 at 17:26
  • @NikolayDyankov I admittedly have no idea why it would work as the parent viewcontroller. – Max Feb 05 '13 at 17:46
  • That was the essence of my question :) – Nikolay Dyankov Feb 05 '13 at 17:48
  • I added registerClass:forCellReuseIdentifier: and it doesn't crash anymore, BUT - when the tableVC is a child I can't set any values for the labels in the cells. Works fine when the table VC is the initial VC. – Nikolay Dyankov Feb 05 '13 at 17:49
  • @NikolayDyankov Strange. I wonder if there's something else going on in the code that's causing that? Either way, I think your safest bet is to use dequeueReusableCellWithIdentifier: and check for the null cell. That way its supported in iOS 6 and below. – Max Feb 05 '13 at 21:03
  • 1
    I ended up slapping a UITableView instead of a child UITableVC and just implemented the protocols. Working so far... (fingers crossed) – Nikolay Dyankov Feb 05 '13 at 21:04