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!
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:
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:
Notice that dequeueReusableCellWithIdentifier: and dequeueReusableCellWithIdentifier:forIndexPath: are different methods.