Thomas Spade
Thomas Spade

Reputation: 744

Populate Table View with an Array of Dictionaries

I'm new to iphone app development and I'm attempting to create an app that uses JSON to parse data from a webservice.

I've been able to get an array of dictionaries that I'm trying to populate two different table views with. The first table view will use data that has an id of 1 and the second will use data that has an id of 2. I need the cell text to be equal to each name of every dictionary that has an id of 1, then I'll use the other data in the dictionary for the detail views. The array looks like this:

(
        {
        "name" = name;
        "email" = email;
        "id" = 1;
    },
        {
        "name" = name2;
        "email" = email2;
        "id" = 1;
    },
        {
        "name" = name;
        "email" = email;
        "id" = 2;   
    },
        {
        "name" = name2;
        "email" = email2;
        "id" = 2;

    }
)

Is there a way to create a new array with all the names that have an id of 1 that I can use to populate the table view?

Thanks, any help is greatly appreciated.

Upvotes: 0

Views: 165

Answers (1)

powerj1984
powerj1984

Reputation: 2226

Untested on the fly code, but the easiest solution is probably just to build two separate arrays, something like:

NSMutableArray* firstArray = [NSMutableArray array];
NSMutableArray* secondArray = [NSMutableArray array];
for (NSDictionary* d in myArray) {
    if ([[d objectForKey:@"id"] isEqualToString:@"1"]) {
        [firstArray addObject:d];
    } else {
        [secondArray addObject:d];
    }
}

Then you can store those two arrays and access them in your table view delegate/datasource methods.

Upvotes: 1

Related Questions