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; }
- iOS利用MJRefresh實(shí)現(xiàn)自定義刷新動(dòng)畫效果
- iOS實(shí)現(xiàn)MJRefresh下拉刷新(上拉加載)使用詳解
- iOS表視圖之下拉刷新控件功能的實(shí)現(xiàn)方法
- iOS功能實(shí)現(xiàn)之列表的橫向刷新加載
- 詳解iOS App中UITableView的創(chuàng)建與內(nèi)容刷新
- iOS開(kāi)發(fā)之UITableView與UISearchController實(shí)現(xiàn)搜索及上拉加載,下拉刷新實(shí)例代碼
- iOS編寫下拉刷新控件
- 詳解iOS開(kāi)發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實(shí)現(xiàn)
- 舉例講解iOS中延遲加載和上拉刷新/下拉加載的實(shí)現(xiàn)
- iOS上下拉刷新控件MJRefresh使用方法詳解
相關(guān)文章
iOS開(kāi)發(fā)-實(shí)現(xiàn)大文件下載與斷點(diǎn)下載思路
本篇文章主要介紹了iOS開(kāi)發(fā)-實(shí)現(xiàn)大文件下載與斷點(diǎn)下載思路,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01iOS實(shí)現(xiàn)簡(jiǎn)易抽屜效果、雙邊抽屜效果
這篇文章主要為大家詳細(xì)介紹了兩款iOS抽屜效果,簡(jiǎn)易抽屜效果、以及雙邊抽屜效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02IOS UIWebView獲取404、504等錯(cuò)誤問(wèn)題解決方案
這篇文章主要介紹了IOS UIWebView獲取404、504等錯(cuò)誤問(wèn)題的相關(guān)資料,并對(duì)相應(yīng)的錯(cuò)誤問(wèn)題提出相應(yīng)的解決方案,需要的朋友可以參考下2016-11-11一個(gè)iOS上的秒表小應(yīng)用的實(shí)現(xiàn)方法分享
這篇文章主要介紹了一個(gè)iOS上的秒表小應(yīng)用的實(shí)現(xiàn)方法分享,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10