views:

307

answers:

1

I have a tableview that is populated by an array. Currently the tableview has no grouping. What I would like to do is check a value of each array object, such as State, and group all the CA items together, all the OR items together, etc. Then, assign those groups a title.

The array is dynamic, and will grow and get new values in the future, so I can't hardcode titles, I would like these to somehow come from my initial array.

Currently I am using the following, but it does not take into account sorting of the array, or if I removed all of the items in the array that have to do with California.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
 if (section == 0) {
  return @"California";
 } else if (section == 1) {
  return @"Washington";
 } else {
  return @"Utah";
 }
}//end tableView

So, I am confusing myself as to how this would be possible. Any tips would be appreciated.

+1  A: 

I would have one array per state/province/etc and put those arrays into a dictionary using the state as the key. Then another array containing all the state keys. Create/update the state key array using keysSortedByValueUsingSelector when you add or remove the dictionary. And some code like this to implement the table delegates...

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [stateKeyArray count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [stateKeyArray objectAtIndex:section];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString* stateKey = [stateKeyArray objectAtIndex:section];
    return [[dataDict objectForKey:stateKey] count];
}

EDIT: These arrays and dictionaries might be declared like this:

// key=state (an NSString), obj=NSMutableArray of AddressRecord objects
NSMutableDictionary* dataDict; 

// sorted array of state keys (NSString)
NSMutableArray* stateKeyArray;   

To add a new address record, something like this should do it. You'd need something similar to remove an address, but you should have enough now to figure that out.

-(void)addAddress:(AddressRecord* addr) {
    NSMutableArray* stateArray = [dataDict objectForKey:addr.state];

    if (stateArray==nil) {
       // adding a new state
       stateArray = [NSMutableArray new];
       [dataDict setObject:stateArray forKey:addr.state];  // add state array to dict
       [stateArray release];

       // update array of state keys (assuming they are NSString)
       self.stateKeyArray = [dataDict keysSortedByValueUsingSelector:@selector(localizedCompare:)];
    }

    [stateArray addObject:addr];   // add address to state array
}
progrmr
Ok, now we are getting somewhere here.Is there a good way of creating separate arrays based off an if statement? So, I could check if each item from my initial array is from a certain state then push those into another array. Just not sure how that would work...
Nic Hubbard
I edited my answer to add some more detail.
progrmr