Code:
#import "ChecklistsViewController.h"
#import "ChecklistItem.h"
@interface ChecklistsViewController ()
@end
@implementation ChecklistsViewController
{
NSMutableArray *items;
}
- (void)viewDidLoad
{
[super viewDidLoad];
items = [[NSMutableArray alloc] initWithCapacity:20];
ChecklistItem *item;
item = [[ChecklistItem alloc] init];
item.text = @"Walk the dog";
item.checked = NO;
[items addObject:item];
item = [[ChecklistItem alloc] init];
item.text = @"Brush my teeth";
item.checked = YES;
[items addObject:item];
item = [[ChecklistItem alloc] init];
item.text = @"Learn iOS development";
item.checked = YES;
[items addObject:item];
item = [[ChecklistItem alloc] init];
item.text = @"Soccer practice";
item.checked = NO;
[items addObject:item];
item = [[ChecklistItem alloc] init];
item.text = @"Eat ice cream";
item.checked = YES;
[items addObject:item];
item = [[ChecklistItem alloc]init];
item.text = @"Gout appointment";
item.checked = YES;
[items addObject:item];
item = [[ChecklistItem alloc]init];
item.text = @"Watch Pats game";
item.checked = NO;
[items addObject:item];
item = [[ChecklistItem alloc]init];
item.text = @"Follow Tutorial";
item.checked = YES;
[items addObject:item];
item = [[ChecklistItem alloc]init];
item.text = @"Vote For Ron Paul";
item.checked = YES;
[items addObject:item];
item = [[ChecklistItem alloc]init];
item.text = @"Listen to JRE";
item.checked = NO;
[items addObject:item];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (void)configureCheckmarkForCell:(UITableViewCell *)cell withChecklistItem:(ChecklistItem *)item
{
if (item.checked) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (void)configureTextForCell:(UITableViewCell *)cell withChecklistItem:(ChecklistItem *)item
{
UILabel *label = (UILabel *)[cell viewWithTag:1000];
label.text = item.text;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];
ChecklistItem *item = [items objectAtIndex:indexPath.row];
[self configureTextForCell:cell withChecklistItem:item];
[self configureCheckmarkForCell:cell withChecklistItem:item];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
ChecklistItem *item = [items objectAtIndex:indexPath.row];
[item toggleChecked];
[self configureCheckmarkForCell:cell withChecklistItem:item];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
This is my code, I'm getting this exception when I try to run & build the app:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Any idea whats going on?
Last edited by a moderator: