0

目前我正在执行以下操作来迭代 NSMutableIndexSet:

 if ([indexSet isNotNull] && [indexSet count] > 0){
        __weak  PNRHighlightViewController *weakSelf = self;
        [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
            if ([[self.highlightedItems_ objectAtIndex:idx] isNotNull]){
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection: [weakSelf.collectionView numberOfSections]-1];
                [weakSelf.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
            }
        }];
    }

我想生成一个 NSIndexPath 数组,然后在这些索引路径处重新加载整个 collectionView。所以基本上我想在块完成后调用重新加载。我该怎么做?

4

3 回答 3

3

一种方法是,

[indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
            if ([[self.highlightedItems_ objectAtIndex:idx] isNotNull]){
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection: [weakSelf.collectionView numberOfSections]-1];
                //store the indexPaths in an array or so
            }
            if (([indexSet count] - 1) == idx) { //or ([self.highlightedItems_ count] - 1)
               //reload the collection view using the above array
            }
        }];
    }
于 2013-01-16T18:34:39.723 回答
1

如果一个方法不要求调度队列或NSOperationQueue块参数运行,并且文档没有另外说明,您通常可以假设它同步执行块。块并不意味着并行性,文档会告诉您块实际上何时异步运行。

NSNotificationCenter的块观察者方法将是异步执行块的方法的示例。在那种情况下,它要求一个NSOperationQueue.

于 2013-01-17T01:55:55.827 回答
0

在块期间构建数组。迭代是同步执行的(所以你也不需要担心弱自我):

 if ([indexSet isNotNull] && [indexSet count] > 0){
        __weak  PNRHighlightViewController *weakSelf = self;

        NSMutableArray *indexPaths = [NSMutableArray new]; // Create your array

        [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
            if ([[self.highlightedItems_ objectAtIndex:idx] isNotNull]){
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection: [weakSelf.collectionView numberOfSections]-1];
                [indexPaths addObject:indexPath];
            }
        }];
        [self.collectionView reloadItemsAtIndexPaths:indexPaths];
    }
于 2013-01-16T19:12:50.567 回答