I need help creating a UITableView with multiple categories.

Discussion in 'iOS Development' started by bamhm182, Jun 15, 2009.

  1. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    I can't for the life of me figure out how to set a TableView that has multiple categories, I can figure out how to add cells just fine, but categories refuse to work no matter what I try.

    I've looked through Apple's source code for a while, but it is completely useless since it doesn't actually create its own arrays, but uses the predefined Time Zones and Regions.

    Can someone help me make categories?

    EDIT: Fair enough. I found the solution here:

    http://www.iphonesdkarticles.com/2009/01/uitableview-creating-simple-table-view.html

    To create a simple TableView, you need to add these:

    ViewController.h
    Code:
    @interface RootViewController : UITableViewController {
    
    NSMutableArray *listOfItems;
    }
    
    @end
    ViewController.m
    Code:
    - (void)viewDidLoad {
    [super viewDidLoad];
    
    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];
    
    //Add items
    
    [listOfItems addObject:@"Cell 1"];
    
    [listOfItems addObject:@"Cell 2"];
    
    [listOfItems addObject:@"Cell 3"];
    
    [listOfItems addObject:@"Cell 4"];
    //add as many cells as you need.
    
    //Set the title
    self.navigationItem.title = @"Your Program's Title";
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listOfItems count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
    NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
    [cell.textLabel setText:cellValue];
    //In their tutorial, you will get a warning if you set it up their way, this is the correct way for 3.0.
    return cell;
    }
    
    //dealloc method declared in RootViewController.m
    - (void)dealloc
    {
    [listOfItems release];
    [super dealloc];
    }
    To have multiple categories, you need something like this:

    ViewController.h
    Code:
    @interface RootViewController : UITableViewController {
    
    NSMutableArray *listOfItems;
    }
    
    @end
    ViewController.m
    Code:
    - (void)viewDidLoad {
    [super viewDidLoad];
    
    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];
    
    NSArray *categoryOneArray = [NSArray arrayWithObjects:@"Cell 1", @"Cell 2", @"Cell 3", @"Cell 4", @"Cell 5", @"Cell 6", @"Cell 7", @"Cell 8", nil];
    NSDictionary *categoryOneDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"YourProgram"];
    
    NSArray *categoryTwoArray = [NSArray arrayWithObjects:@"Cell 9", @"Cell 10", nil];
    NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:@"YourProgram"];
    
    [listOfItems addObject:categoryOneDict];
    [listOfItems addObject:categoryTwoDict];
    
    //You can set as many categories as you like. Just make sure they're set the same way as the two above.
    
    //Set the title
    self.navigationItem.title = @"Countries";
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return [listOfItems count];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    //Number of rows it should expect should be based on the section
    NSDictionary *dictionary = [listOfItems objectAtIndex:section];
    NSArray *array = [dictionary objectForKey:@"YourProgram"];
    return [array count];
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if(section == 0)
    return @"Category 1";
    else
    return @"Category 2";
    }
    
    //If you have more than 2 categories, you can add them like this:
    //if(section == 1)
    //if(section == 2)
    //etc...
    //The first Category is 0, the 2nd is 1, the 3rd is 2, etc.
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
    
    //First get the dictionary object
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"Countries"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    [cell.textLabel setLabel:cellValue];
    
    return cell;
    }
    
    If you follow their tutorial and get a warning where it says

    cell.text = cellValue;

    that is because the correct way to define it in 3.0 is:

    [cell.textLabel setLabel:cellValue];
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    In the future, do not edit your post in an attempt to "remove it." Instead, leave it alone and post the solution in a new post in the same thread.

    The reason is that other people may have the same problem as you had, and will find your thread using search. If there is no pertinent information, it makes their search for answers harder.

Share This Page