HurkNburkS
HurkNburkS

Reputation: 5510

Sectioned UITableView using NSMutableArray of NSDictionaries

I have an NSMutableArray of NSDictionaries that looks like this when logged

<__NSArrayM 0x165bcfc0>(
{
    SeqN = 122;
    Code = 024;
    Number = 1;
},
{
    SeqN = 132;
    Code = 030;
    Number = 1;
},
{
    SeqN = 124;
    Code = 002;
    Number = 2;
},
{
    SeqN = 114;
    Code = 035;
    Number = 3;
},
{
    SeqN = 144;
    Code = 009;
    Number = 3;
},
{
    SeqN = 444;
    Code = 022;
    Number = 3;
}
)

I would like to know the best way to create a UITableView using the Number as sections, so the UITableView would look like this

Number 1
SeqN 122, Code 024
SeqN 132, Code 030
Number 2
SeqN 124, Code 002
Number 3
SeqN 114, Code 035
SeqN 144, Code 009
SeqN 444, Code 022

I am just lost, I am not sure if I need to spilt the NSMutableArray of NSDictionaries into an Array of Arrays? Or is there an easier solution that can be achieved using something I don't know?

I have managed to create a Unique array of Number that I am using for my sectionCount return. I don't know how to return how many rows per section or split the array so the correct data is being displayed.

Update

This is how I created an NSDictionary of NSArrays from my NSMutableArray of NSDictionaries

 NSMutableDictionary *sortedDictionaryArraysForTableView = [NSMutableDictionary dictionary];

    for (NSDictionary *dict in xmlMArray) {
        NSString *currNumber = [dict objectForKey:@"Number"];
        if(![[sortedDictionaryArraysForTableView allKeys] containsObject:currNumber])
            sortedDictionaryArraysForTableView[currNumber] = [NSMutableArray array];
        [sortedDictionaryArraysForTableView[currNumber] addObject:dict];
    }

Upvotes: 0

Views: 80

Answers (2)

David Berry
David Berry

Reputation: 41236

Reorganize your data so that it looks like:

@[
     @{
        @"number":@(1),
        @"items":@[
            @{ @"seqn": @"114", @"code": @"035", @"number:@(1) },
....
     @},
@]

basically you want an array of dictionaries, each containing an array of dictionaries (each of which represents your current item)

Upvotes: 0

Wain
Wain

Reputation: 119031

Convert your array of dictionaries into a dictionary of arrays of dictionaries where the keys are the Numbers and the values are an array of all of the dictionaries which contain that Number.

The count of the dictionaries is now the number of sections. You can use allKeys to get all of the numbers (and sort it so you can get the section key and associated array of items).

Upvotes: 1

Related Questions