@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的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];
});
- (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;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有