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 - |
测试,问题完美解决。