iOS UICollectionView reloadData后找到某个cell 更新某个cell数据
来源:互联网 发布:mac打开csgo黑屏 编辑:程序博客网 时间:2024/05/20 21:45
在开发中有个需求, 要在刷新完数据后再修改其中某些cell的属性, 结果发现是有问题的
[self.collectionView reloadData]; UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath]; cell.backgroundColor = [UIColor redColor];
debug发现在执行完reloadData方法后并没有立即刷新cell, 而是执行了下面对cell操作的代码, 当这个方法执行完成后reloadData才会去刷新数据, 从而导致我们对cell的操作又被刷新的时候修改了
要解决这个问题 ,可以这么操作
[self.collectionView reloadData]; NSMutableArray *indexPaths = [NSMutableArray array]; for (int i = 0; i < 10 ; i++) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; [indexPaths addObject:indexPath]; } [self.collectionView performBatchUpdates:^{ [self.collectionView reloadItemsAtIndexPaths:indexPaths]; } completion:^(BOOL finished) { for (NSIndexPath *indexPath in indexPaths) { UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath]; cell.backgroundColor = [UIColor redColor]; } }];
如果操作的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 -
0 0
- iOS UICollectionView reloadData后找到某个cell 更新某个cell数据
- 如何在UICollectionView reloadData后找到的某个cell
- 如何在UICollectionView reloadData后找到的某个cell
- 如何在UICollectionView reloadData后找到的某个cell
- UITableView 更新指定的某个Cell
- iOS UICollectionView 和 UITableView cell更新 详解
- Swift3.0 UITableView/UICollectionView默认选中某个 cell
- iOS 默认某个cell为选中状态
- iOS 刷新 UITableView某个 section 或 cell
- iOS刷新某个cell时候crash
- iOS刷新某个cell时候crash
- iOS刷新tableView某个cell或section
- iOS 刷新某个Section或Cell
- TableView删除某个cell
- Excel 过滤选择数据后,某个Cell显示其值,并自定义合计的VBA.
- iOS --- UICollectionView中使用reloadItemsAtIndexPaths进行局部cell更新
- iOS tableview的第二页数据刷新特定某个cell数据的解决办法
- ios UICollectionView的cell重叠
- 场景二:synchronized修饰方法
- monkeyrunner自动化测试工具--录制回放
- Spring随笔3
- android软键盘的属性设置
- Android小记:EditText的两个常用属性
- iOS UICollectionView reloadData后找到某个cell 更新某个cell数据
- xampss,远程访问phpmyadmin数据库
- 智能车上位机与下位机通信的例子
- 田忌赛马 364nyoj
- 配置bower组件保存位置
- Linux和Windows下ping
- 屏幕适配
- 矩阵/向量迭代算法:PageRank, HITS, SPEAR
- LeetCode 001 Two Sum