Search code examples
iphoneobjective-cios5xcode4

Xcode4: Terminating app due to uncaught exception 'NSInternalInconsistencyException'


I am getting this Exception in xcode4 with the iphone simulator:

Terminating app due to uncaught exception 'NSInternalInconsistencyException'

It seems to be caused if you return no objects(return 0) back in the cellForRowAtIndex method. But I have mine set to return the cell.

Can anyone see what may be causing this error? Here is a bunch of screenshots:

Controller Configuration: My Controller configuration

Prototype cell prototype cell 1

attentionCell: enter image description here

Storyboards: Full view of the storyboards

Full Error:

**Assertion failure in -[UITableView 
_createPreparedCellForGlobalRow:withIndexPath:],           
/SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
2012-07-30 14:34:16.660 Simple Storyboard[263:f803] Terminating app due 
to uncaught exception 'NSInternalInconsistencyException', 
reason: 'UITableView dataSource must return a cell from 
tableView:cellForRowAtIndexPath:'**

Here is the code:

#import "LWWBidTaskListController.h"

@interface LWWBidTaskListController ()
@property (strong, nonatomic)NSArray *tasks;

@end

@implementation LWWBidTaskListController

@synthesize tasks;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.tasks = [NSArray arrayWithObjects: @"Walk the dog", @"URGENT:Buy milk", @"Clean     hidden lair", @"Invent miniature dolphins", @"Find new henchmen", @"Get revenge on do-gooder heroes", @"URGENT: Fold laundry", @"Hold entire hold hostage", @"Manicure", nil];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this     view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.tasks = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
//return 0;
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
//return 0;
return [tasks count];
}

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


NSString *identifier = nil;
NSString *task = [self.tasks objectAtIndex:indexPath.row];
NSRange urgentRange = [task rangeOfString:@"URGENT"];
if (urgentRange.location == NSNotFound)
{
    identifier = @"plainCell";
}
else 
{
    identifier = @"attentionCell";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

// Configure the cell...

UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
cellLabel.text = task;

return cell;
}

What does that error mean and how can I fix it?


Solution

  • It looks as though you never create a cell. If you have never allocated and initialized a cell then there are none to dequeue. before the // Configure the cell.. line you need to check if a reusable cell was dequeued and if not, create one like so:

    if (cell == nil) {
      cell = [[UITableViewCell alloc]  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
      // Configure common elements here
    
    }
    
      // Now go on to configure specific elements for this row
    

    As you are using storyboard, perhaps you have not set up the prototype cell attributes correctly. The identifiers must match those in your method above.

    See this pic for where you find the prototype cell attributes Prototype cell setup. it is from this tutorial that may help you as well Ray Wenderlich and Storyboards