views:

54

answers:

2

This is kinda confusing so please bear with me.

I have an array (listOfStates) of dictionaries. Where listOfStates has this structure:

{stateName} - {stateCapital}
{Alabama} - {Montgomery}
{Alaska} - {Juneau}
{Arizona} - {Phoenix}
...
{West Virginia} - {Charleston}
{Wisconsin} - {Madison}
{Wyoming} - {Cheyenne}

This is the code used to obtain the 1st letter of each US State(stateName) in order to group them together alphabetically:

listOfStates = [[NSArray alloc] init];  
stateIndex = [[NSMutableArray alloc] init];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];

for (NSDictionary *row in listOfStates) {
    [tempArray addObject:[row valueForKey:@"stateName"]];

     for (int i=0; i<[tempArray count]-1; i++){
     char alphabet = [[tempArray objectAtIndex:i] characterAtIndex:0];
     NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet];
         if (![stateIndex containsObject:uniChar]){  
             [stateIndex addObject:uniChar];
         }
     }
}

That code works beautifully, however I'm having issues understanding how to populate a tableview cell with BOTH the @"stateName" and @"stateCapital"(as the subtitle) after I use NSPredicate to sort the array.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
    //---get the letter in the current section---
NSString *alphabet = [stateIndex objectAtIndex:[indexPath section]];

    //---get all states beginning with the letter---
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSArray *states = [[listOfStates valueForKey:@"stateName"] filteredArrayUsingPredicate:predicate];

        //---extract the relevant state from the states object---
    cell.textLabel.text = [states objectAtIndex:indexPath.row];
        //cell.textLabel.text = [[listOfStates objectAtIndex:indexPath.row] objectForKey:@"stateName"];
        //cell.detailTextLabel.text = [[listOfStates objectAtIndex:indexPath.row] objectForKey:@"stateCapital"];

return cell;
}

Any help is appreciated and I am more than willing to send the entire project, if you think it'll help you understand.

Thank you so much.

+1  A: 
cell.textLabel.text = [[states objectAtIndex:indexPath.row] objectForKey:@"stateName"];
cell.detailTextLabel.text =[states objectAtIndex:indexPath.row] objectForKey:@"stateCapital"];

Update:

Unfortunately, the 'states' array only includes the state names...

Change the predicate and filter to:

NSPredicate *p=[NSPredicate predicateWithFormat:@"stateName beginswith[c] %@", alphabet];
NSArray *states=[listOfStates filteredArrayUsingPredicate:p];

...that will give you an array of states that all start with the same letter. Then you can sort the array and the row will give you the right state dictionary.

TechZen
Much to my chagrin, that doesn't work as it should. That was the first thing I tried.
dbarrett
Unfortunately, the 'states' array only includes the state names, the captials are dropped when the array is created by NSPredicate.
dbarrett
A: 

Maybe change the data structure so you have one dictionary with state name as a key and state capital as a value. That way you can sort your keys and get the value using the keys, not indexes.

Steam Trout
Not sure how to do that as the initial array is generated via a SQLite query. Suggestions on how to do [email protected]
dbarrett
`+ [NSDictionary dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys];`That should do it.
Steam Trout