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

问题:collection view 在reloadData之后,找不到 cell

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

于是我写了如下代码,

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

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

解决方案

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

UICollectionView 有一个方法,

- (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。更新代码,问题解决。

[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。代码如下,

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 -

测试,问题完美解决。


欢迎来我的个站逛逛: http://alexyu.me/


引用
stackoverflow : Animation Duration for UICollectionView selectItemAtIndexPath:animated:scrollPosition:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视...
    lakerszhy阅读 3,925评论 1 22
  • 最近将 UICollectionView 进行了一个全面的学习及总结,参考了网上大量的文章,把官方文档进行了大概翻...
    varlarzh阅读 21,557评论 3 94
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,246评论 4 61
  • 人要是有了追求,就一样了。 莫言追求文学梦,他获得了诺贝尔文学奖。 李安追求电影梦,他独身一人去美国,拍了《卧虎藏...
    东风东风阅读 701评论 0 6