2013-01-16 21 views
0

目前我做了以下迭代上NSMutableIndexSet:通過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。所以基本上我想在塊完成後調用重裝。我該怎麼做?要做到這一點是

回答

2

的一種方式,

[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 
      } 
     }]; 
    } 
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]; 
    } 
+0

你確定它是同步的? – adit

+0

從這個意義上講,在枚舉完成之後,枚舉之後的代碼纔會被執行。 – jrturton

+0

是什麼讓你覺得它不是? – jrturton

1

如果方法不要求調度隊列或NSOperationQueue一個塊參數運行並且文檔不會另有說明,您通常可以假設它同步執行塊。塊並不意味着並行性,並且文檔會告訴你什麼時候塊實際上是異步運行的。

NSNotificationCenter的塊觀察者方法將是異步執行塊的方法的一個例子。在這種情況下,它要求NSOperationQueue

+0

正是!許多人認爲有一些神奇的規則,因爲代碼是在一個塊中,它將在一個單獨的線程上運行。 「塊不意味着平行」是一個偉大的句子,我可能會得到這件T恤。 – jrturton