Return to Snippet

Revision: 41742
at February 23, 2011 02:21 by bethjt1220


Initial Code
// ---------- Top Level ----------------
- (void)viewDidLoad {
    
    self.title = @"Categories";
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"CategoryData"
                                                     ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc]
                          initWithContentsOfFile:path];
    self.buildings = dict;
    [dict release];
    
    for(id name in buildings) {
        CategorySecondLevelViewController *controller = [[CategorySecondLevelViewController alloc]
                                                         initWithStyle:UITableViewStylePlain];
        controller.title = name;
                
        [array addObject:controller];
        [controller release];    
    }
    
    self.categories = array;
    [array release];
    
    [super viewDidLoad];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CategoryCell = @"CategoryCell";
    
    // reuse cell if possible
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             CategoryCell];
    if(cell == nil) {
        cell = [[[UITableViewCell alloc]
                 initWithStyle: UITableViewCellStyleDefault
                 reuseIdentifier: CategoryCell] autorelease];
    }
    
    // configure cell
    NSUInteger row = [indexPath row];
    
    // extension point to include images if wanted
    UITableViewController *controller = [categories objectAtIndex:row];
    cell.textLabel.text = controller.title;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

// ---------- Second Level ----------------
- (void)viewDidLoad {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"CategoryData"
                                                     ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc]
                          initWithContentsOfFile:path];
    
    NSString *key = self.title;
    NSArray *values = [dict objectForKey:key];

    self.list = values;
    [dict release];
    
    [super viewDidLoad];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *BuildingIdentifier = @"BuildingIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             BuildingIdentifier];
    if(cell == nil) {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault
                 reuseIdentifier:BuildingIdentifier] autorelease];
    }
    
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [list objectAtIndex:row];

    return cell;
}

Initial URL


Initial Description
Pulls data from a plist set up as a dictionary of arrays of strings.  
Creates multi-level TableView that can be nested inside a navigation controller.
Snippet shows key methods for 2 nested levels.
2 levels --> 2 classes (both of which extend UITableViewController)

Initial Title
IOS TableViews in Navigation Controller

Initial Tags
ios

Initial Language
iPhone