Search code examples
iosuitableviewsigabrtuncaught-exception

Getting SIGABRT error with simple storyboard app


I have a problem that I've searched the internet for a solution for with no avail. I'm following along in chapter 10 of Apress' Beginning iOS 5 development which is on storyboards.

I'm thinking the book is missing a simple piece of code and since I'm going through this for the first time, I don't know how to resolve it.

I've restarted the project twice, but keep getting this error in the debugger:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

#import "BIDTaskListController.h"

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

@implementation BIDTaskListController

@synthesize tasks;

- (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 world hostage",
                  @"Manicure",
                  nil];
}

- (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);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    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];

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

    return cell;
}
@end

Solution

  • lack of code:

    ...

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds];
        label.tag = 1;
        [cell addSubview:label];
    }
    
    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
    cellLabel.text = task;   
    

    ....