Iterating through an NSArray and storing items in groups of 12

I've definitely tried to do my due diligence on this one but keep coming up short. I have an array of objects that I have parsed and I want to iterate through these and store them. Assuming the array is 144 objects (just an example), I want to store it in groups of 12 to display in a tableview cell. Actually of those 12 objects in the array I'll likely only be displaying 3-4 in the cell, but all of those objects in the detail view.

To help explain what I mean (sorry if it hasn't made sense at this point) here's some of the code I've got that is getting the data.

NSMutableArray *objectsArray = [[NSMutableArray alloc] initWithCapacity:0];

for (TFHppleElement *element in objectsNode) {
    PHSingleEvent *singleEvent = [[PHSingleEvent alloc]init];
    [objectsArray addObject:singleEvent];

    singleEvent.title = [[element firstChild] content];

This pulls down the entire array of objects (an unknown number but definitely a multiple of 12). How would I go about storing 12 objects at a time into a single event?

I can log the info with

PHSingleEvent *firstObject = [objectsArray objectAtIndex:0] // this one is null
PHSingleEvent *eventStartTime = [objectsArray objectAtIndex:1];
PHSingleEvent *eventEndTime = [objectsArray objectAtIndex:2];
PHSingleEvent *lastObject = [objectsArray objectAtIndex:11];

NSLog(@"single object of event: %@", eventStartTime.startTime);
NSLog(@"single object of event: %@", eventEndTime.endTime);


But the array keeps going past 12. I want to iterate up through each 12 objects and store those values, preferably as strings to be displayed in a cell and detail view.

Any ideas?

Thanks much in advance and I will be here to answer any questions if I was unclear.



How about using a for loop? Assuming that each event object has 12 sub-objects (i.e. indices 0 - 11) you could achieve storing it by using a mod function. For example:

NSMutableArray *eventArray = [[NSMutableArray alloc] init];
for(int i=0; i<objectArray.count/12;i++){
    int offset = 12*i;
    NSMutableArray *event = [objectsArray subarrayWithRange:NSMakeRange(offset, 12)];
    [eventArray addObject:event];

So now eventArray has n arrays, each of 12 objects (where n = totalObjects/12)

EDIT: A better idea would be to use NSDictionary. For example:

NSMutableArray *eventArray = [[NSMutableArray alloc] init];
    for(int i=0; i<objectArray.count/12;i++){
        int offset = 12*i;
        NSDictionary *tempDict = [[NSDictionary alloc] initWithObjectsAndKeys: [objectsArray objectAtIndex: offset], @"eventStartTime", [objectsArray objectAtIndex: offset+1], @"eventEndTime", ..., [objectsArray objectAtIndex: offset+11, @"lastObject",nil]; 
        [eventArray addObject:tempDict];

Then you can access each of the above objects using a similar statement as shown below:

PHSingleEvent *eventStartTime = [[eventArray objectAtIndex: index] objectForKey: @"eventStartTime"];

Hope this helps

This method will return an array of smaller arrays based on the group size you specify.

- (NSMutableArray*)makeGroupsOf:(int)groupSize fromArray:(NSArray*)array
    if (!array || array.count == 0 || groupSize == 0)
        return nil;

    NSMutableArray *bigGroup = [[NSMutableArray alloc] init];

    for (int i = 0; i < array.count; )
        NSMutableArray *smallGroup = [[NSMutableArray alloc] initWithCapacity:groupSize];
        for (int j = 0; j < groupSize && i < array.count; j++)
            [smallGroup addObject:[array objectAtIndex:i]];
        [bigGroup addObject:smallGroup];
    return bigGroup;

I haven't tested it or anything though. After you have the big array with the smaller array(s) it is just a matter of filling each cell with any desired number of objects from the sub arrays.

Note: You might want to handle the cases when the array is empty, null or the group size is 0 differently.

Need Your Help

scrolling gets disabled after setContentOffset:animated:, what am i doing wrong?

objective-c ios uiscrollview

I want to programmatically scroll a specific amount of pixels. Changing the content offset inside the scrollViewDidScroll method i can do this but when it stops at the desired point it stops scroll...