Objective-C: How to convert NSIndexSet to NSIndexPath

烂漫一生 提交于 2020-06-26 05:03:34

问题


I would like to convert NSIndexSet Object to NSIndexPath,as effect as link: https://developer.apple.com/reference/photos/phphotolibrarychangeobserver?language=objc

- (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];
        }
    }
});
}

Anybody know the implementation of the method indexPathsFromIndexSet:in the exsample? thanks!


回答1:


You can convert you NSIndexSet into IndexPath array with the help of enumerator of IndexSet like below :

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);


来源:https://stackoverflow.com/questions/42217639/objective-c-how-to-convert-nsindexset-to-nsindexpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!