欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

一,點擊下載按鈕后,調(diào)用的時afnetworking的downLoad方法,具體代碼如下

創(chuàng)新互聯(lián)建站2013年至今,公司以做網(wǎng)站、網(wǎng)站設(shè)計、系統(tǒng)開發(fā)、網(wǎng)絡(luò)推廣、文化傳媒、企業(yè)宣傳、平面廣告設(shè)計等為主要業(yè)務,適用行業(yè)近百種。服務企業(yè)客戶1000多家,涉及國內(nèi)多個省份客戶。擁有多年網(wǎng)站建設(shè)開發(fā)經(jīng)驗。為企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、創(chuàng)意設(shè)計、宣傳推廣等服務。 通過專業(yè)的設(shè)計、獨特的風格,為不同客戶提供各種風格的特色服務。

@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;
    // 開一個異步線程,放到主隊列里面刷新數(shù)據(jù)
    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{
  // 修改對應的數(shù)據(jù)源
  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];
}

問題:如果是但一個下載刷新是可以的,但是多個任務同時進行的話,就會來回的數(shù)據(jù)交換

解決方法一:在網(wǎng)上查了好多資料,發(fā)現(xiàn)是不能實時刷新cell的,不管是單個還是多個,因為刷新會出現(xiàn)界面跳動的現(xiàn)象,當然是不是有其他的方法可以解決,這也是有可能的。

解決方法二:直接在異步里面實時賦值(找到相應的cell),這樣就可以避免因刷新cell帶來的界面跳動的現(xiàn)象,具體看代碼:

但是這樣還存在了,刷新時已經(jīng)下載了的cell進度條會出現(xiàn)歸零的現(xiàn)象,刷新過后會還原到正常值,然而,如果是下載完事了再刷新,直接就是0了,這應該是cell復用導致的,那么接下來就來解決刷新歸零的問題。

// 找到相應的cell的indexPath
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
MyTableViewCell *cell = (MyTableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
    dispatch_async(dispatch_get_main_queue(), ^{
  // 這樣網(wǎng)上說這樣會耗費cpu資源,我親測后,基本不費資源,還有就是怕內(nèi)存泄露等問題,但是現(xiàn)在還沒撲捉到,以后發(fā)現(xiàn)不妥之處了,再加修正
      cell.progress.progress = _progress;
//      [self reloadCell:index];
    });

下面是cell復用的機制,如果在里面不給進度條付初值,就不會在刷新的時候出現(xiàn)歸零的問題

- (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;
}

網(wǎng)頁題目:iOStableView上拉刷新顯示下載進度的問題及解決辦法
新聞來源:http://chinadenli.net/article36/godspg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計、網(wǎng)站維護、網(wǎng)站內(nèi)鏈、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)