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

首頁 > 系統 > iOS > 正文

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

2019-10-21 18:48:08
字體:
來源:轉載
供稿:網友

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


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中阳县| 从江县| 房产| 长顺县| 汉寿县| 车致| 安阳县| 河西区| 布拖县| 金塔县| 色达县| 长岛县| 井研县| 京山县| 海淀区| 大冶市| 铜鼓县| 谢通门县| 福海县| 股票| 牙克石市| 红原县| 抚宁县| 奇台县| 义乌市| 满洲里市| 榆社县| 马公市| 昆明市| 沈阳市| 基隆市| 洛阳市| 西吉县| 穆棱市| 伊宁县| 布尔津县| 朝阳县| 托里县| 沾益县| 桓台县| 宜黄县|