国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS tableView上拉刷新顯示下載進度的問題及解決辦法

2020-07-26 02:55:04
字體:
來源:轉載
供稿:網友

一,點擊下載按鈕后,調用的時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的indexPathNSIndexPath *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;}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 韶关市| 凉城县| 兴城市| 容城县| 武安市| 丹东市| 桃江县| 含山县| 公主岭市| 怀远县| 北京市| 杭锦旗| 新晃| 惠来县| 独山县| 葫芦岛市| 万源市| 塔城市| 西华县| 宁乡县| 噶尔县| 饶阳县| 曲阜市| 涟源市| 紫云| 大同市| 许昌市| 凉城县| 堆龙德庆县| 垣曲县| 濮阳市| 九寨沟县| 洪泽县| 宁强县| 阜新市| 合江县| 抚远县| 莱阳市| 阜康市| 石台县| 曲沃县|