简体   繁体   中英

Group element in NSMutableArray which contains object

I have an NSMutableArray that contains an object of a class model in each position like this. The class model contains 2 types of information, which we will call id and name . So, in every location of my NSMutableArray I have an object that contains 2 information.

Then, in the first position of my NSMutableArray I have

{
 id = 1;
 name = "Dan"; //this is the first object in NSMutableArray
}

In the second position of NSMutableArray, I have:

{
 id = 1;
 name = "Luca"; 
}

In the third position

{
 id = 2;
 name = "Tom"; 
}

and so on..

Ok, my goal is to make the union of identical IDs between the various objects within the SNMutableArray but it's too difficult!

For example, if I have:

{
 id = 1;
 name = "Tom"; 
}

{
 id = 1;
 name = "Luca"; 
}

{
 id = 2;
 name = "Steve"; 
}

{
 id = 2;
 name = "Jhon"; 
}

{
 id = 3;
 name = "Andrew"; 
}

The goal is:

{
 id = 1;
 name = "Tom"; 
 name = "Luca"; 
}
{
 id = 2;
 name = "Steve";
 name = "Jhon"; 

}
{
 id = 3;
 name = "Andrew"; 
}

Any ideas? would like to use this in the cellForRowAtIndexPath method and I tried to write this: (cm is my class model and myArray is the NSMutableArray which contains an object of cm class)

ClassModel *cm = [myArray objectAtIndex:indexPath.row];


NSMutableArray * resultArray = [NSMutableArray new];
NSArray * groups = [array valueForKeyPath:cm.ID];
for (NSString * groupId in groups)
{
     NSMutableDictionary * entry = [NSMutableDictionary new];
     [insert setObject: groupId forKey: @ "groupId"];

     NSArray * groupNames = [array filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @ "groupId =% @", groupId]];
     for (int i = 0; i <groupNames.count; i ++)
     {
         NSString * name = [[groupNames objectAtIndex: i] objectForKey: @ "name"];
         [entry setObject: name forKey: [NSS string stringWithFormat: @ "name% d", i + 1]];
     }
     [resultArray addObject: entry];
}

NSLog (@ "% @", resultArray);

But this does not work..maybe because each element in my array is an object?? .. Help!

You have the right basic idea, but you shouldn't try and do this in cellForRowAt . Rather, you need to create a new array that has the data in the required structure and use that array as the source for your tableview. You will also need to create a new class to put in the array; one that has an id and an NSMutableArray for the names (I won't show this but I will call it GroupClassModel )

Use something like:

NSMutableDictionary *groups = [NSMutableDictionary new]
for (ClassModel *cm in array) {
    GroupClassModel *gcm = groups[cm.id];
    if (gcm == nil) {
        gcm = [GroupClassModel new];
        gcm.id = cm.id
        groups[cm.id] = gcm
    }
    [gcm.names addObject:cm.name];
}

NSArray *groupedName = [groups allValues];
// Finally, sort groupedName by id if that is required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM