tableView methods and continents array


#1

Man, this chapter seems to be throwing me for a loop.

I’m finding that for tableView:numberOfRowsInSection: and tableView:numberOfSectionsInTableView I’m having to check the count before getting items out of the continents array.

With the implementation in the book, I get this error:
2010-07-23 09:09:11.004 Nayshunz[15020:207] getting title for header in section
2010-07-23 09:09:11.005 Nayshunz[15020:207] *** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)’

  • (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
    NSLog(@“getting title for header in section”);
    NSDictionary *continentDict = [continents objectAtIndex: section];
    NSLog(@“we’re good”);
    return [continentDict objectForKey:@“name”];
    }

If I wrap a check, like below, things run ok:

  • (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
    {
    NSLog(@“getting title for header in section”);
    if ( section < [continents count] )
    {
    NSDictionary *continentDict = [continents objectAtIndex: section];
    NSLog(@“we’re good”);
    NSArray *nations = [continentDict objectForKey:@“list”];
    return [nations count];
    } else
    {
    return 0;
    }
    }

I’m feeling like I’m totally missing a step or something here. I new up the continents array in the init method, but the only place items are added to it is in the - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText method afaict.