如何在UICollectionView reloadData后找到的某个cell

collection view 在reloadData之后,找不到 cell

今天碰到的一个问题:app 主界面是一个 collection view,当数据源增加一个数据时,我需要立即刷新视图,并打开相应的 cell。

于是我写了如下代码,

1
2
3
[self.collectionView reloadData];
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
[self performSegueWithIdentifier:openInFileIdentifier sender:cell];

debug 发现获取的 cell 是 nil,在断点处查看 collection view 的 visibleCells 也是空的。我猜测 reloadData 背后开启了分线程来处理这个事情,所以 reloadData 方法返回的时候,视图并没有完成刷新。

解决方案

去 stackOverFlow 上搜索一下,找到了一个解决方案:

UICollectionView 有一个方法,

1
- (void)performBatchUpdates:(void (^)(void))updates completion:(void (^)(BOOL finished))completion; // allows multiple insert/delete/reload/move calls to be animated simultaneously. Nestable.

这个方法会在第一个 block 中处理 insert/delete/reload/move 等操作,等操作完成之后会执行第二个block。更新代码,问题解决。

1
2
3
4
5
6
7
[self.collectionView reloadData];
[self.collectionView performBatchUpdates:^{
    [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
} completion:^(BOOL finished) {
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    [self performSegueWithIdentifier:openInFileIdentifier sender:cell];
}];

如果 cell 不在视图内呢?

如果 cell 刚好不在屏幕区域内,处于 visible 的状态,那么用上述方法依然会找不到 cell。
我想可以先让 collection view 滚动到目标 cell 处,然后在打开 cell。这里需要用到 scroll 的代理方法,以在滚动动画结束的时候,找到目标 cell。(滚动结束才能确定目标 cell 是 visible 的。)你还需要定义一个 indexPath 值以区分滚动结束的时候是否需要打开cell,以及要打开哪个 cell。代码如下,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
NSIndexPath *needOpenCellIndexPath;

- (void)foo
{
    needOpenCellIndexPath = ...;
    [self.collectionView performBatchUpdates:^{
        [self.collectionView reloadItemsAtIndexPaths:@[needOpenCellIndexPath]];
    } completion:^(BOOL finished) {
        UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:needOpenCellIndexPath];
        if (cell) {
            [self performSegueWithIdentifier:RPAOpenMapSegueIdentifier sender:cell];
        } else {
            // Start scrolling to the target cell.
            [self.collectionView selectItemAtIndexPath:needOpenCellIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];
        }    
    }];
}

#pragma mark - scroll view deleagte
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    if (needOpenCellIndexPath) {
        UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:needOpenCellIndexPath];
        [self performSegueWithIdentifier:openInFileIdentifier sender:cell];
    }
    needOpenCellIndexPath = nil;
}
#pragma mark -

测试,问题完美解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值