NSArray Loop to grab Objects

纵饮孤独 提交于 2019-12-06 07:58:04

It would be good if you can preprocess this but if you can't do that for some reason then this is what you should do,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    MPSection * section = [self.sections objectAtIndex:section];
    NSInteger   count   = [section.items count];

    return ((count % 2 == 0)? count / 2 : (count / 2 + 1) );
}

And in tableView:cellForRowAtIndexPath: method,

/* */

MPSection *section = [self.sections objectAtIndex:indexPath.section];

id item1 = [section.items objectAtIndex:(indexPath.row * 2)];
id item2 = ((indexPath.row * 2 + 1) < [section.items count])? [section.items objectAtIndex:(indexPath.row * 2 + 1)] : nil;

/* Use item1 & item2 to fill both the subviews */

Original Answer

Use the NSEnumerator instance for this purpose

NSEnumerator *enumerator = [section.items objectEnumerator];
id item1, item2;
while ( (item1 = [enumerator nextObject]) && (item2 = [enumerator nextObject]) ) {
    // Use item1 & item2
}

As such I think you must be getting index out of bounds error for the snippet you mentioned.

Overkill

There seems to be some question about performance so I tested the three suggested methods and timed them in a loop where I log them.

Enumerator, Fast Enumeration with Object Search, For Loop (50000 elements): 19.253626, 88.269961, 18.767572
Enumerator, Fast Enumeration with Object Search, For Loop (25000 elements): 9.164311, 25.105664, 8.777443
Enumerator, Fast Enumeration with Object Search, For Loop (10000 elements): 3.428265, 6.035876, 3.144609
Enumerator, Fast Enumeration with Object Search, For Loop (5000 elements): 2.010748, 2.548562, 1.980477
Enumerator, Fast Enumeration with Object Search, For Loop (1000 elements): 0.508310, 0.389402, 0.338096
Enumerator, Fast Enumeration with Object Search, For Loop (500 elements): 0.156880, 0.163541, 0.150585
Enumerator, Fast Enumeration with Object Search, For Loop (100 elements): 0.076625, 0.034531, 0.036576
Enumerator, Fast Enumeration with Object Search, For Loop (50 elements): 0.026115, 0.022686, 0.041745

From the looks of it, @Caleb's for loop approach might be the best approach to take.

If you're dead set on using fast enumeration here, you could set a flag that lets you skip each even iteration of the loop:

BOOL doThisOne = YES;
NSArray itemArray = [section itemArray];
for (id item in items) {
    if (doThisOne) {
        item1 = item;
        item2 = [itemArray objectAtIndex:1+[itemArray indexOfObject:item]];
    }
    doThisOne = !doThisOne;
}

Note: The code above throws a range exception if the number of items in the array is odd. Some of the other answers avoid this, but I think the best answer is simply not to use fast enumeration here.

It'd be so much simpler to use an enumerator, or just use a regular old for loop:

NSEnumerator *e = [[section items] objectEnumerator];
while (item = [e nextObject]) {
    item1 = item;
    item2 = [e nextObject];
}

or:

NSArray *itemArray = [section items];
for (int i = 0; (i + 1) < [items count]; i += 2) {
    item1 = [items objectAtIndex:i];
    item2 = [items objectAtIndex:i+1];
}
for(id item in array){
    if([array indexOfObject:item] % 2 != 0){
     item1 = item;
    }else{
     item2 = item;
    }
}
int numItems = [section.items count];

for(int i = 0; i < count; i++) {
    item1 = [section.items objectAtIndex: i];
    item2 = ((i + 1) < count)) ? [section.items objectAtIndex: (i + 1)] : nil;
} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!