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

Posted

技术标签:

【中文标题】Objective-C:如何将 NSIndexSet 转换为 NSIndexPath【英文标题】:Objective-C: How to convert NSIndexSet to NSIndexPath 【发布时间】:2017-02-14 04:00:40 【问题描述】:

我想将 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:方法的实现吗?谢谢!

【问题讨论】:

google.com/search?q=indexPathsFromIndexSet Tks,在这里实现工作。 - (NSArray *)indexPathsFromIndexSet:(NSIndexSet *)indexSet NSMutableArray *indexPaths = [NSMutableArray array]; [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:0]; [indexPaths addObject:indexPath]; ]; return [indexPaths copy]; 【参考方案1】:

您可以借助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);

【讨论】:

谢谢!你的回答很有效,但我不能给你点赞,因为我的声誉很低。 @CodeChanger swift中没有enumerateIndexesUsingBlock函数

以上是关于Objective-C:如何将 NSIndexSet 转换为 NSIndexPath的主要内容,如果未能解决你的问题,请参考以下文章

如何将 .hour() 和 .minute() 从 Swift 转换为 Objective-C?

如何将此 Objective-C 代码放入按钮操作中?

如何将此方法从 Objective-C 转换为 Swift?

如何将 RxSwift 用于 Objective-C 和 Swift 项目?

Objective-C:如何将以下 JSON 转换为可用数据

如何将这个 Objective-C 代码片段写入 Swift?