I’m just starting out to learn iOS development. I’m doing the “Start Developing iOS Apps Today” tutorial and I’m stuck on the section “Add Data”.
After setting the Table view to use ‘Dynamic Prototypes’, and setting the identifier to ‘ListPrototypeCell’, I added the method ‘cellForRowAtIndexPath’ but it’s crashing with these errors:
Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle> (not loaded)
2014-08-10 13:35:50.519 ToDoList[8954:60b] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:5439
2014-08-10 13:35:50.523 ToDoList[8954:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier ListPrototypeCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
. . . .
libc++abi.dylib: terminating with uncaught exception of type NSException
I’ve been following the tutorial exactly and I can’t find the mistake. Can anyone suggest what I’m doing wrong?
The code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
return cell;
}
Use:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell"];
For storyboards that's what you want. Also follow it up if necessary with:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ListPrototypeCell"];
}
Edit: If instead, you want to continue using dequeueReusableCellWithIdentifier:forIndexPath:, then in your controller's initialization you should use registerClass:forCellReuseIdentifier:, as per the docs if you are not using a storyboard. Also the (cell == nil) part has become unnecessary in the latest Xcode versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With