TableView滑动时不加载网络图片,停止滑动下载图片

最近优化项目中,TableView滑动时不加载,停止滑动才加载图片,在iOS项目开发中是非常常见的。
本文章中就不重复说比较简单的和一些细节方面的问题了,只说几个重要的地方。好了废话不说了,上代码!!!
  • 一.在Model模型中添加一个属性,
    /**是否加载网络图片*/
    @property(nonatomic,assign)BOOL isLoad;
    Model的注意事项
    -(void)setValue:(id)value forUndefinedKey:(NSString *)key
    {
        你懂得
    }
    
  • 二.在自定义Cell.h 中声明一个方法
    -(void)setImageWithModel:(HomeViewModel *)model;
    
  • 自定义的Cell.m中实现方法
    -(void)setImageWithModel:(HomeViewModel *)model
    {
       if (model == nil) {
           [self.iconImgView setImage:[UIImage imageNamed:@"约单头像加载"]];
      }else{
    [self.iconImgView wxl_setImageWithURL:[OSSImageKit scale_w80_h80:model.headPhotoUrl] placeholder:@"约单头像加载" completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [_iconImgView setContentScaleFactor:[[UIScreen mainScreen] scale]];
        _iconImgView.contentMode =  UIViewContentModeScaleAspectFill;
        _iconImgView.clipsToBounds  = YES;
        
        [model setIsLoad:YES];//加载网络图片//下载
     }];
    }
    

    }

  • 三.回到Controller中,在我们请求数据的时候,Model额外的设置属性
    for (NSDictionary *homeDic in dic[@"data"][@"lease"]) {
    
              HomeViewModel *model =[[HomeViewModel alloc] init];
    
              [model setValuesForKeysWithDictionary:homeDic];
    
              model.isLoad = NO;//装到数组中,先不下载
    
              [self.dataSource addObject:model];
              
              dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
              //让tableView准备好后,再显示
                  [self loadShowCells];
              });
          }
    
  • 四.cellForRow方法里要做个判断了
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        HomePublishSkillViewCell * cell = [tableView dequeueReusableCellWithIdentifier:homePublishIdentifier];
         if (!cell) {
              cell = [[HomePublishSkillViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:homePublishIdentifier];
          }
          HomeViewModel * model = self.dataSource[indexPath.row];
       
          if (model.isLoad) {
               [cell setImageWithModel:model];
          }else{
               [cell setImageWithModel:nil];
          }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.seperateLine.hidden =  indexPath.row == self.dataSource.count - 1 ?YES:NO;
    
        return cell;
    }
    
  • 五.cellForRow 也做好判断了,真正要监听是否加载网络图片的时候到了
pragma mark -- 监听滚动事件
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
ZZLog(@"----%d",decelerate);

  if (!decelerate) {
       //滑动时,加载占位图
       [self loadShowCells];
   }
}
pragma mark -- 监听滚动事件
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

             [self loadShowCells];
}
pragma mark -- 加载Cell中网络头像图片
-(void)loadShowCells{
      NSArray * array = [self.mainTableView indexPathsForVisibleRows];
  for (NSIndexPath *indexPath in array) {

   HomePublishSkillViewCell * cell = (HomePublishSkillViewCell *)[self.mainTableView cellForRowAtIndexPath:indexPath];
        HomeViewModel * model = self.dataSource[indexPath.row];
        [cell setImageWithModel:model];
    
  }

}
  • 至此,有啥不对的地方还请大神们,多多指点!!本人参照 TableView滑动不加载重新整理了一下。

你可能感兴趣的:(TableView滑动时不加载网络图片,停止滑动下载图片)