欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS tableView上拉刷新顯示下載進(jìn)度的問(wèn)題及解決辦法

 更新時(shí)間:2017年03月01日 11:21:29   作者:haha_hello  
這篇文章主要介紹了 iOS tableView上拉刷新顯示下載進(jìn)度的問(wèn)題及解決辦法,需要的朋友可以參考下

一,點(diǎn)擊下載按鈕后,調(diào)用的時(shí)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;
    // 開(kāi)一個(gè)異步線程,放到主隊(duì)列里面刷新數(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{
  // 修改對(duì)應(yīng)的數(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];
  // 刷新某一個(gè)cell
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}

問(wèn)題:如果是但一個(gè)下載刷新是可以的,但是多個(gè)任務(wù)同時(shí)進(jìn)行的話,就會(huì)來(lái)回的數(shù)據(jù)交換

解決方法一:在網(wǎng)上查了好多資料,發(fā)現(xiàn)是不能實(shí)時(shí)刷新cell的,不管是單個(gè)還是多個(gè),因?yàn)樗⑿聲?huì)出現(xiàn)界面跳動(dòng)的現(xiàn)象,當(dāng)然是不是有其他的方法可以解決,這也是有可能的。

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

但是這樣還存在了,刷新時(shí)已經(jīng)下載了的cell進(jìn)度條會(huì)出現(xiàn)歸零的現(xiàn)象,刷新過(guò)后會(huì)還原到正常值,然而,如果是下載完事了再刷新,直接就是0了,這應(yīng)該是cell復(fù)用導(dǎo)致的,那么接下來(lái)就來(lái)解決刷新歸零的問(wèn)題。

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

下面是cell復(fù)用的機(jī)制,如果在里面不給進(jìn)度條付初值,就不會(huì)在刷新的時(shí)候出現(xiàn)歸零的問(wè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;
}

相關(guān)文章

最新評(píng)論