当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS tableView上拉刷新显示下载进度的问题及解决办法

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

钟轩宇,arialnarrow,丫头换心

一,点击下载按钮后,调用的时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;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网