iOS开发实战小知识——UICollectionView刷新闪屏

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

之前都是直接请求完数据直接刷新
[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;
 }];


当然,最简单的是关闭UICollectionView刷新的隐式动画,解决问题。
reloadData时关闭动画。

- (void)refreshData {
    [CATransaction setDisableActions:YES];
    [self.collectionView reloadData];
    [CATransaction commit];
}

猜你喜欢

转载自blog.csdn.net/u013712343/article/details/126748755