在UITableview中使用NSURLSession和进度条动画进行多次下载

时间:2015-02-18 05:58:03

标签: download

我要在tableview中显示文件列表。每行包含下载按钮。 当我点击下载按钮时,它应该显示循环进度条,如appstore iphone中的那个下载,一旦下载完成,我将在下载按钮上显示勾选图标。下载我使用NSURLSession与downloadtaskwithurl。它还可以同时下载多个文件。

这里我使用带有UIcontrol的循环进度条。     为uitableview创建了customcell,并在tableview中添加了progressbar。

但循环进度条不起作用。也无法在UITableview中显示多个下载。它仅显示选定单元格中没有进度的进度条。如果我选择多次下载,则表格视图显示最后一次单击的单元格中的进度条。任何帮助

这是使用

的代码
-(void)didload{NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"identifier"];
    defaultConfigObject.timeoutIntervalForRequest=10.0;
    defaultConfigObject.HTTPMaximumConnectionsPerHost = 4;
    NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
    [myQueue setMaxConcurrentOperationCount:4];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: nil];
    NSURL *url = [NSURL URLWithString:signedUrlStr];
    NSURLSessionDownloadTask *downloadTask=[defaultSession downloadTaskWithURL:url];
    [downloadTask resume];}


- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
      didWriteData:(int64_t)bytesWritten
 totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {

    CGFloat progMax = (float)totalBytesExpectedToWrite;
    CGFloat prog = (float)totalBytesWritten;

    NSLog(@"data length %lu",(unsigned long)totalBytesWritten);
    NSLog(@"Siva %lu",(unsigned long)totalBytesExpectedToWrite);

    cell.progressbar.value=0;
    cell.progressbar.maxValue=progMax;
    cell.progressbar.value=prog;

    dispatch_async(dispatch_get_main_queue(), ^(void){

        [_tableMonth reloadData];
    });
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error {
    if (error) {
        // Handle error

        NSDictionary *userInfo = [error userInfo];
        NSError *underlyingError = [userInfo objectForKey:NSUnderlyingErrorKey];
        NSString *underlyingErrorDescription = [underlyingError localizedDescription];
        NSInteger underlyingErrorCode = [underlyingError code];

        NSLog(@"Error desc %@",underlyingErrorDescription);
        NSLog(@"Error code %li",(long)underlyingErrorCode);

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:[Language get:@"retrykey" alter:@"title not found"]
                                                      delegate:self
                                             cancelButtonTitle:[Language get:@"okaykey" alter:@"title not found"]
                                             otherButtonTitles: nil];
        [alert show];
    }
    else {
        NSLog(@"File download completed");
    }
}

1 个答案:

答案 0 :(得分:0)

Try this code to update progress bar:

     [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates];