iOS tableView上拉刷新显示下载进度的问题及解决办法
一,点击下载按钮后,调用的时afnetworking的downLoad方法,具体代码如下
@interfaceViewController()<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=[NSURLURLWithString:@"http://song.90uncle.com/upload/media/e366a607d222442f83ed7028c4d7118e_20170227110100.mp4"];
NSURLRequest*request=[NSURLRequestrequestWithURL:URL];
AFHTTPSessionManager*manger=[AFHTTPSessionManagermanager];
manger.responseSerializer=[AFJSONResponseSerializerserializer];
_downloadTask=[mangerdownloadTaskWithRequest:requestprogress:^(NSProgress*_NonnulldownloadProgress){
NSLog(@"%f",downloadProgress.fractionCompleted);
_progress=downloadProgress.fractionCompleted;
//开一个异步线程,放到主队列里面刷新数据
dispatch_async(dispatch_get_main_queue(),^{
[selfreloadCell:index];
});
}destination:^NSURL*(NSURL*targetPath,NSURLResponse*response){
//获取沙盒cache路径
NSURL*documentsDirectoryURL=[[NSFileManagerdefaultManager]URLForDirectory:NSCachesDirectoryinDomain:NSUserDomainMaskappropriateForURL:nilcreate:NOerror:nil];
return[documentsDirectoryURLURLByAppendingPathComponent:[responsesuggestedFilename]];
}completionHandler:^(NSURLResponse*response,NSURL*filePath,NSError*error){
if(error){
NSLog(@"失败");
}else{
NSLog(@"成功");
}
}];
[_downloadTaskresume];
}
-(void)reloadCell:(NSInteger)index{
//修改对应的数据源
NSMutableDictionary*dic=[[NSMutableDictionaryalloc]init];
[dicaddEntriesFromDictionary:self.dataSource[index]];
[dicremoveObjectForKey:@"progress"];
[dicsetObject:@(_progress)forKey:@"progress"];
[self.dataSourcereplaceObjectAtIndex:indexwithObject:dic];
//刷新某一个cell
NSIndexPath*indexPath=[NSIndexPathindexPathForRow:indexinSection:0];
[self.tableViewreloadRowsAtIndexPaths:[NSArrayarrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationNone];
}
问题:如果是但一个下载刷新是可以的,但是多个任务同时进行的话,就会来回的数据交换
解决方法一:在网上查了好多资料,发现是不能实时刷新cell的,不管是单个还是多个,因为刷新会出现界面跳动的现象,当然是不是有其他的方法可以解决,这也是有可能的。
解决方法二:直接在异步里面实时赋值(找到相应的cell),这样就可以避免因刷新cell带来的界面跳动的现象,具体看代码:
但是这样还存在了,刷新时已经下载了的cell进度条会出现归零的现象,刷新过后会还原到正常值,然而,如果是下载完事了再刷新,直接就是0了,这应该是cell复用导致的,那么接下来就来解决刷新归零的问题。
//找到相应的cell的indexPath
NSIndexPath*indexPath=[NSIndexPathindexPathForRow:indexinSection:0];
MyTableViewCell*cell=(MyTableViewCell*)[_tableViewcellForRowAtIndexPath:indexPath];
dispatch_async(dispatch_get_main_queue(),^{
//这样网上说这样会耗费cpu资源,我亲测后,基本不费资源,还有就是怕内存泄露等问题,但是现在还没扑捉到,以后发现不妥之处了,再加修正
cell.progress.progress=_progress;
//[selfreloadCell:index];
});
下面是cell复用的机制,如果在里面不给进度条付初值,就不会在刷新的时候出现归零的问题
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{
MyTableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
cell.selectionStyle=NO;
cell.title.text=self.dataSource[indexPath.row][@"title"];
//cell.progress.progress=[self.dataSource[indexPath.row][@"progress"]floatValue];
returncell;
}