Search code examples
iphonejsonuitableviewextjssections

UITableView JSON Data sorting for sections


In this gist is JSON data that I use in my UITableView: https://gist.github.com/786829

I take the data marked INPUT and reformat it to OUTPUT such that I can display it acurately with sections in the UITableView. This is done with this code:

groups = [parsedData objectForKey:@"venues"];

NSArray * distinctTypes = [groups valueForKeyPath:@"@distinctUnionOfObjects.type"];

output = [NSMutableArray array];

  for (NSString * type in distinctTypes) {
    NSPredicate * filter = [NSPredicate predicateWithFormat:@"type = %@", type];
    NSMutableDictionary *group = [[NSMutableDictionary alloc] init];
    [group setObject:type forKey:@"type"];

    [group setObject:[groups filteredArrayUsingPredicate:filter] forKey:@"venues"];
    [output addObject:group];
}

Is there a better way to do this? The INPUT is currently used for a sencha app list, that does this grouping automatically.


Solution

  • Are you having a specific problem or just asking about best practices? This looks fine to me, just don't forget to release your group dictionary after you add it to the output array.