开发者

UITableView JSON Data sorting for sections

开发者 https://www.devze.com 2023-02-05 08:32 出处:网络
In this gist is JSON data that I use in my UITableView: https://gist.github.c开发者_运维知识库om/786829

In this gist is JSON data that I use in my UITableView: https://gist.github.c开发者_运维知识库om/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.


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.

0

精彩评论

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