iOS tableView上拉刷新显示下载进度的问题及解决办法

一,点击下载按钮后,调用的时afnetworking的downLoad方法,具体代码如下

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
  XLCircleProgress *_circle;
  CGFloat _progress;
}
@property (strong,nonatomic) NSURLSessionDownloadTask *downloadTask;
@property (strong,nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSMutableArray *dataSource;
@end
- (void)request:(NSInteger)index{
  //下载
  NSURL *URL = [NSURL URLWithString:@"http://song.90uncle.com/upload/media/e366a607d222442f83ed7028c4d7118e_20170227110100.mp4"];
  NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
  manger.responseSerializer = [AFJSONResponseSerializer serializer];
  _downloadTask= [manger downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
    NSLog(@"%f",downloadProgress.fractionCompleted);
    _progress = downloadProgress.fractionCompleted;
    // 开一个异步线程,放到主队列里面刷新数据
    dispatch_async(dispatch_get_main_queue(), ^{
      [self reloadCell:index];
    });
  } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    //获取沙盒cache路径
    NSURL * documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
  } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    if (error) {
      NSLog(@"失败");
    } else {
       NSLog(@"成功");
    }
  }];
  [_downloadTask resume];
}
- (void)reloadCell:(NSInteger)index{
  // 修改对应的数据源
  NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
  [dic addEntriesFromDictionary:self.dataSource[index]];
  [dic removeObjectForKey:@"progress"];
  [dic setObject:@(_progress) forKey:@"progress"];
  [self.dataSource replaceObjectAtIndex:index withObject:dic];
  // 刷新某一个cell
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}

问题:如果是但一个下载刷新是可以的,但是多个任务同时进行的话,就会来回的数据交换

解决方法一:在网上查了好多资料,发现是不能实时刷新cell的,不管是单个还是多个,因为刷新会出现界面跳动的现象,当然是不是有其他的方法可以解决,这也是有可能的。

解决方法二:直接在异步里面实时赋值(找到相应的cell),这样就可以避免因刷新cell带来的界面跳动的现象,具体看代码:

但是这样还存在了,刷新时已经下载了的cell进度条会出现归零的现象,刷新过后会还原到正常值,然而,如果是下载完事了再刷新,直接就是0了,这应该是cell复用导致的,那么接下来就来解决刷新归零的问题。

// 找到相应的cell的indexPath
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
MyTableViewCell *cell = (MyTableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
    dispatch_async(dispatch_get_main_queue(), ^{
  // 这样网上说这样会耗费cpu资源,我亲测后,基本不费资源,还有就是怕内存泄露等问题,但是现在还没扑捉到,以后发现不妥之处了,再加修正
      cell.progress.progress = _progress;
//      [self reloadCell:index];
    });

下面是cell复用的机制,如果在里面不给进度条付初值,就不会在刷新的时候出现归零的问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  cell.selectionStyle = NO;
  cell.title.text = self.dataSource[indexPath.row][@"title"];
//  cell.progress.progress = [self.dataSource[indexPath.row][@"progress"] floatValue];
  return cell;
}

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。