views:

35

answers:

1

Hi all,

I have a dictionary that hold address details, sometimes it can hold, 'name, email, telephone' other times, just 'name, mobile'

name = "Someone";
email = "[email protected]";
telephone = "01000 000000";

or

name = "Someone Else";
mobile = "07700 000000";

I want to display these in a detail table view, in my head this makes sense (I know the below code doesn't work, but I don't know any other way of displaying what I want to happen):

if(selectedData objectForKey=@"name" at indexPath.row){
        cell.textLabel.text = @"Name";
        cell.detailTextLabel.text = [selectedData objectForKey:@"name"];
    } else if(selectedData objectForKey=@"email" at indexPath.row){
        cell.textLabel.text = @"Email";
        cell.detailTextLabel.text = [selectedData objectForKey:@"email"];
    } else if(selectedData objectForKey=@"telephone" at indexPath.row){
        cell.textLabel.text = @"Telephone";
        cell.detailTextLabel.text = [selectedData objectForKey:@"telephone"];
    } else if(selectedData objectForKey=@"mobile" at indexPath.row){
        cell.textLabel.text = @"Mobile";
        cell.detailTextLabel.text = [selectedData objectForKey:@"mobile"];
    } 

But I can't seem to get this coded correctly, and I might even be barking up the wrong tree! Any help or pointers on this welcome.

A: 

I have managed to sort this by first adding an array of all the keys within viewdidload:

arrayOfKeys = [[NSMutableArray alloc]init];
    for (NSString *key in [selectedData allKeys]){
         NSLog(@"Key:%@", key);
        if(key != @"title" && key != @"name"){
            [arrayOfKeys addObject:key];
        }
    }

Then within cellForRowAtIndexPath I used:

NSString *currentKey = [arrayOfKeys objectAtIndex:indexPath.section]; 
    if(currentKey == @"email"){
        cell.textLabel.text = @"Email";
        cell.detailTextLabel.text = [selectedData objectForKey:@"email"];
    } else if(currentKey== @"telephone"){
        cell.textLabel.text = @"Telephone";
        cell.detailTextLabel.text = [selectedData objectForKey:@"telephone"];
    } else if(currentKey == @"mobile"){
        cell.textLabel.text = @"Mobile";
        cell.detailTextLabel.text = [selectedData objectForKey:@"mobile"];
    } else if(currentKey == @"company"){
        cell.textLabel.text = @"Company";
        cell.detailTextLabel.text = [selectedData objectForKey:@"company"];
    }

PS. don't forget to set 'arrayOfKeys' in your header file.

jimbo