2017-02-14 56 views
1

我想的NSIndexSet對象轉換爲NSIndexPath,如爲紐帶作用:https://developer.apple.com/reference/photos/phphotolibrarychangeobserver?language=objc的Objective-C:如何轉換的NSIndexSet到NSIndexPath

- (void)photoLibraryDidChange:(PHChange *)changeInfo { 
// Photos may call this method on a background queue; 
// switch to the main queue to update the UI. 
dispatch_async(dispatch_get_main_queue(), ^{ 
    // Check for changes to the displayed album itself 
    // (its existence and metadata, not its member assets). 
    PHObjectChangeDetails *albumChanges = [changeInfo changeDetailsForObject:self.displayedAlbum]; 
    if (albumChanges) { 
     // Fetch the new album and update the UI accordingly. 
     self.displayedAlbum = [albumChanges objectAfterChanges]; 
     self.navigationController.navigationItem.title = self.displayedAlbum.localizedTitle; 
    } 

    // Check for changes to the list of assets (insertions, deletions, moves, or updates). 
    PHFetchResultChangeDetails *collectionChanges = [changeInfo changeDetailsForFetchResult:self.albumContents]; 
    if (collectionChanges) { 
     // Get the new fetch result for future change tracking. 
     self.albumContents = collectionChanges.fetchResultAfterChanges; 

     if (collectionChanges.hasIncrementalChanges) { 
      // Tell the collection view to animate insertions/deletions/moves 
      // and to refresh any cells that have changed content. 
      [self.collectionView performBatchUpdates:^{ 
       NSIndexSet *removed = collectionChanges.removedIndexes; 
       if (removed.count) { 
        [self.collectionView deleteItemsAtIndexPaths:[self **indexPathsFromIndexSet**:removed]]; 
       } 
       NSIndexSet *inserted = collectionChanges.insertedIndexes; 
       if (inserted.count) { 
        [self.collectionView insertItemsAtIndexPaths:[self indexPathsFromIndexSet:inserted]]; 
       } 
       NSIndexSet *changed = collectionChanges.changedIndexes; 
       if (changed.count) { 
        [self.collectionView reloadItemsAtIndexPaths:[self indexPathsFromIndexSet:changed]]; 
       } 
       if (collectionChanges.hasMoves) { 
        [collectionChanges enumerateMovesWithBlock:^(NSUInteger fromIndex, NSUInteger toIndex) { 
         NSIndexPath *fromIndexPath = [NSIndexPath indexPathForItem:fromIndex inSection:0]; 
         NSIndexPath *toIndexPath = [NSIndexPath indexPathForItem:toIndex inSection:0]; 
         [self.collectionView moveItemAtIndexPath:fromIndexPath toIndexPath:toIndexPath]; 
        }]; 
       } 
      } completion:nil]; 
     } else { 
      // Detailed change information is not available; 
      // repopulate the UI from the current fetch result. 
      [self.collectionView reloadData]; 
     } 
    } 
}); 
} 

任何人都知道在exsample方法indexPathsFromIndexSet:的實施?謝謝!

+0

https://www.google.com/search?q=indexPathsFromIndexSet – vadian

+0

韓國社交協會,FPGA實現這裏工作。 ' - (NSArray *)indexPathsFromIndexSet:(NSIndexSet *)indexSet {NS_MarrayArray * indexPaths = [NSMutableArray array]; (NSUInteger idx,BOOL * _Nonnull stop)NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0]; [indexPaths addObject:indexPath]; }]; return [indexPaths copy]; }' – JohnChen

回答

3

您可以NSIndexSet轉換你進入IndexPath陣列的IndexSet枚舉的幫助象下面這樣:

NSMutableArray *allIndexPaths = [[NSMutableArray alloc] init]; 

[indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) 
    { 
     NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:0]; 
     [allIndexPaths addObject:indexPath]; 
}]; 

NSLog(@"All Index Path : %@",allIndexPaths); 
+0

謝謝!你的回答工作,但我不能給你我的低聲譽。 @CodeChanger – JohnChen

相關問題