开发者社区 问答 正文

不用block遍历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,也就是block完成之后调用重新加载,不知道怎么实现?

展开
收起
爵霸 2016-03-26 09:17:39 2261 分享 版权
1 条回答
写回答
取消 提交回答
  • 遍历是同步执行的,所以在block期间遍历数组:

     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];
        }
    2019-07-17 19:15:15
    赞同 展开评论
问答地址: