I’m now learning UITableView class on iOS development. I want to implement three sections in my tableView, but it always crashes when I tap the tabBarItem which contains the tableView. My code is below:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"Settings";
self.tabBarItem.image = [UIImage imageNamed:@"19-gear.png"];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *list1 = [[NSMutableArray alloc] initWithObjects:@"fbfb", @"dfhbfh", @"rtjtj", @"rgrrth", @"grhh", @"tyjtyj", nil];
NSArray *list2 = [[NSMutableArray alloc] initWithObjects:@"fgngg", @"yjyj", @"rhrht", @"rthfh", @"tjtyjyj", @"fsfsdf", nil];
NSArray *list3 = [[NSMutableArray alloc] initWithObjects:@"24242", @"24242", @"24242", @"4242", @"4242", @"24242", nil];
self.array1 = list1;
self.array2 = list2;
self.array3 = list3;
[list1 release];
[list2 release];
[list3 release];
}
#pragma mark -
#pragma mark - TableView Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return [array1 count];
break;
case 1:
return [array2 count];
break;
case 2:
return [array3 count];
break;
default:
break;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cells = @"Cells";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cells];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cells];
}
switch (indexPath.section) {
case 0:
cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
break;
case 1:
cell.textLabel.text = [array2 objectAtIndex:indexPath.row];
break;
case 2:
cell.textLabel.text = [array3 objectAtIndex:indexPath.row];
break;
default:
break;
}
return cell;
}
the crash log is below:
2012-06-25 23:33:37.694 X-Factor[617:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SettingsViewController 0x6c912a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'
*** First throw call stack:
(0x13cd022 0x155ecd6 0x13ccee1 0x9c5022 0x936f6b 0x936edb 0x951d50 0x23971a 0x13cedea 0x13387f1 0x23826e 0xde1fc 0xde779 0xde99b 0xfa0a9 0xf9edd 0xf84aa 0xf834f 0xf9e14 0x13cee99 0x1a14e 0x1a0e6 0x2424bd 0x13cee99 0x1a14e 0x1a0e6 0xc0ade 0xc0fa7 0xc0b13 0x244c48 0x13cee99 0x1a14e 0x1a0e6 0xc0ade 0xc0fa7 0xc0266 0x3f3c0 0x3f5e6 0x25dc4 0x19634 0x12b7ef5 0x13a1195 0x1305ff2 0x13048da 0x1303d84 0x1303c9b 0x12b67d8 0x12b688a 0x17626 0x21c2 0x2135)
terminate called throwing an exception(lldb)
A few points:
a) I recommend you use ARC rather than manual memory management (as does Apple). ARC is the future of Objective-C and the sooner you start thinking about object graphs rather than retain/release the better.
b) Don’t use properties in your init code, use the ivars instead (code assumes you synthesise as underscore for your ivars)
c) You need to return the number of sections in the tableview (3) rather than 2 you have entered
d) Your tableView:numberOfRowsInSection method in not consistent. Put a break statement in each switch statement. i.e.: