Objective-C:如何将 NSIndexSet 转换为 NSIndexPath

标签 objective-c nsindexpath nsindexset

我想将 NSIndexSet 对象转换为 NSIndexPath,效果如链接: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];
        }
    }
});
}

有人知道示例中 indexPathsFromIndexSet: 方法的实现吗?谢谢!

最佳答案

您可以在 IndexSet 的枚举器的帮助下将 NSIndexSet 转换为 IndexPath 数组,如下所示:

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

关于Objective-C:如何将 NSIndexSet 转换为 NSIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42217639/

相关文章:

ios - objective-c : Use a array to point to another array

ios - NSIndexpath.item 与 NSIndexpath.row

cocoa - NSOutlineView 子类崩溃 - NSRangeException

ios - 在 Swift 中从整数数组创建 NSIndexSet

ios - 如何从 NSURLSessionDataTask 获取 NSDictionary

objective-c - 双击 NSOutlineView 的标题是否触发双击方法?

iphone - 在 View 之间传递 IndexPath

swift - 尝试访问 IndexSet 中的某些元素

iphone - 更改 UIPicker 外观

ios - 为什么我刚创建的指针抛出 KERN_INVALID_ADDRESS?