【发布时间】:2011-09-22 20:23:46
【问题描述】:
我目前正在开发一个应用程序,该应用程序需要能够从 NSArray 中获取两个对象,然后将其存储在另一个对象中。
我目前正在进行这个快速枚举循环,
NSUInteger count = 0;
NSUInteger i = count + 1;
for (id item in [section items]) {
item1 = [section.items objectAtIndex:count];
item2 = [section.items objectAtIndex:i];
count++;
}
现在,我要做的是抓取第一个位置的对象并存储在 item1 中,然后将第二个位置存储在 item2 中。下次它通过循环时,我希望它将对象存储在 item1 的第三个位置,然后是 item2 的第四个位置,依此类推。
有没有人尝试过并实现了这一点?
编辑
这就是我目前所拥有的,我认为最好我更深入地解释我正在做的事情,所以就到这里吧。这是我首先拥有的代码,稍后我会解释。
MPSection *section = [self.sections objectAtIndex:indexPath.section];
NSArray *itemArray = [section items];
for (NSUInteger i = 0; (i + 1) < [section.items count]; i += 2) {
item1 = [itemArray objectAtIndex:i];
item2 = [itemArray objectAtIndex:i+1];
}
如您所见,这是在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中运行的,因为我想获取通常会在 UITableView 的第一行和第二行中显示的内容,并将其放入一个单元格中,该单元格分为两个子视图。
我发现,通过使用上面的代码,它肯定没有这样做。有没有更简单的方法可以做到这一点,如果是这样,有人可以告诉我这一点。我真的需要通过将内存保留和时间消耗保持在最低限度来解决这个问题。
【问题讨论】:
-
MPSection *section = [self.sections objectAtIndex:indexPath.row];正确吗?还是你的意思是MPSection *section = [self.sections objectAtIndex:indexPath.section];?
标签: objective-c ipad object for-loop nsarray