开发者

iPhone tableview: titleForHeaderInSection derived from array

开发者 https://www.devze.com 2022-12-29 05:01 出处:网络
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 togeth

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 t开发者_如何转开发ogether, 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.


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号