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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值