ios - Iterating through an NSArray and storing items in groups of 12 -


i've tried due diligence on 1 keep coming short. have array of objects have parsed , want iterate through these , store them. assuming array 144 objects (just example), want store in groups of 12 display in tableview cell. of 12 objects in array i'll displaying 3-4 in cell, of objects in detail view.

to explain mean (sorry if hasn't made sense @ point) here's of code i've got getting data.

nsmutablearray *objectsarray = [[nsmutablearray alloc] initwithcapacity:0];  (tfhppleelement *element in objectsnode) {     phsingleevent *singleevent = [[phsingleevent alloc]init];     [objectsarray addobject:singleevent];      singleevent.title = [[element firstchild] content]; } 

this pulls down entire array of objects (an unknown number multiple of 12). how go storing 12 objects @ time single event?

i can log info with

phsingleevent *firstobject = [objectsarray objectatindex:0] // 1 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);  etc... 

but array keeps going past 12. want iterate through each 12 objects , store values, preferably strings displayed in cell , detail view.

any ideas?

thanks in advance , here answer questions if unclear.

c.

how using loop? assuming each event object has 12 sub-objects (i.e. indices 0 - 11) achieve storing using mod function. 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 eventarray has n arrays, each of 12 objects (where n = totalobjects/12)

edit: better idea use nsdictionary. 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 can access each of above objects using similar statement shown below:

phsingleevent *eventstarttime = [[eventarray objectatindex: index] objectforkey: @"eventstarttime"]; 

hope helps


Comments