iOS 处理collectionview刷新闪屏的问题

collectionview下拉加载更多的时候,总是闪屏,搞得很难受,今天着手调整一下,直接上代码

之前都是直接请求完数据直接刷新
[self.collectionview reloadData];

现在需要做的是只刷新新添加的cell
1.创建一个一个可变数组,获取新增的cell的indexpath,添加到数组中

  NSMutableArray*indexPaths = [[NSMutableArray alloc] init];
   // resultArr新获取到数据的数组
 for (NSDictionary *modelDic in resultArr) {
      ZQWcollevtionModel *model = [[ZQWcollevtionModel alloc] init];
      [model setValuesForKeysWithDictionary:modelDic];
      //self.collectionGengduoView.dataListArr  这是我的数据源,自己注意替换         
      [self.collectionGengduoView.dataListArr addObject:model];
       //这里要计算对了,因为我是放在model添加数组之后的,所以-1        
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow: self.collectionGengduoView.dataListArr.count - 1 inSection: 0];
    //把获取到的indexpath添加到新建的数组中
      [indexPaths addObject: indexPath];
         
  }
 2.把新获取到cell插入到collectionview中,刷新新获取到的cell              
  if(indexpaths.count > 0) {    
      //把新获取到的cell插入到collectionview中      
        [self.collectionGengduoView insertItemsAtIndexPaths:indexPaths];

        [UIView performWithoutAnimation:^{
        //刷新新获取到的cell
        [self.collectionGengduoView reloadItemsAtIndexPaths:indexPaths];

               }];
}

到这里就结束了,希望可以帮助到你。

其实再刷新的时候做一下渐渐显示的动画效果会更好,还是直接上代码

在给cell赋值的时候,setmodel的方法里面
//把image的透明度设为0
 _imgView.alpha = 0;
//给一个0.5秒的动画,把透明度在设为1,效果会好一点
[UIView animateWithDuration:0.5 animations:^{
  _imgView.alpha = 1;
 }];
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容