我正在尝试构建一个应用程序,该应用程序将显示可以下载并阅读的杂志集合。我想要发生的是,如果杂志没有下载,点击单元格时将开始下载,并且在显示下载进度的相应单元格中将显示进度视图。我已分类UICollectionViewCell
并添加了UIProgressView
属性。到目前为止,我已经能够检查文件是否被下载,如果不是,下载它,如果是,只需使用PDF library called VFR Library
打开它。我也可以更新放置在UICollectionViewCell
之外的进度视图,但我无法更新放置在UICollectionViewCell
内的一个视图。不幸的是,我下载和更新progressview的大部分逻辑都在didSelectItemAtIndexPath:
之内。我知道这并不高雅,但我的经验水平还不够高,我无法有效地开发自己的课程,以便无缝地协作。但是一旦我明白了这一点,我将努力完善代码并将其抽象出来。无论如何,这是我在didSelectItemAtIndexPath:
更新UICollectionViewCell中的UIProgressView
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
IssueCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
self.navBarLabel.hidden = YES;
self.mainProgressView.hidden = NO;
self.view.userInteractionEnabled = NO;
//self.activityIndicator.hidden = NO;
[self.activityIndicator startAnimating];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"issue%ld", (long)indexPath.row]]stringByAppendingPathExtension:@"pdf"];
if ([[NSFileManager defaultManager]fileExistsAtPath:path])
{
ReaderDocument *document = [ReaderDocument withDocumentFilePath:path password:nil];
if (document != nil)
{
//opens PDF for viewing
ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
readerViewController.delegate = self;
readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:readerViewController animated:YES completion:nil];
self.mainProgressView.hidden = YES;
self.navBarLabel.hidden = NO;
self.view.userInteractionEnabled = YES;
//self.activityIndicator.hidden = YES;
[self.activityIndicator stopAnimating];
}
}
else
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlArray[indexPath.row]]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
ReaderDocument *document = [ReaderDocument withDocumentFilePath:path password:nil];
if (document != nil)
{
//opens PDF for viewing
ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
readerViewController.delegate = self;
readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:readerViewController animated:YES completion:nil];
self.mainProgressView.hidden = YES;
self.navBarLabel.hidden = NO;
//self.activityIndicator.hidden = YES;
self.view.userInteractionEnabled = YES;
[self.activityIndicator stopAnimating];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Download Failed" message:@"There was a problem downloading this issue" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alertView show];
self.mainProgressView.hidden = YES;
self.navBarLabel.hidden = NO;
[self.activityIndicator stopAnimating];
//self.activityIndicator.hidden = YES;
self.view.userInteractionEnabled = YES;
NSLog(@"Error: %@", error);
}];
[operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
float percentDone =((float)((int)totalBytesWritten)/(float)((int)totalBytesExpectedToWrite));
self.mainProgressView.progress = percentDone;
[cell.progressView setProgress:percentDone];
[self.collectionView reloadData];
// [cell.progressView performSelectorOnMainThread:@selector(loadingProgress:) withObject:[NSNumber numberWithFloat:percentDone] waitUntilDone:NO];
NSLog(@"Sent %lld of %lld bytes, %@", totalBytesWritten, totalBytesExpectedToWrite, path);
}];
[operation start];
}
}
我这样做是没有办法,我可以在这个方法中更新相应的单元格内的进步观点还是有别的东西,我需要做的就是它的工作任何方式?谢谢你提供的所有帮助。
感谢您的回复。不幸的是,我无法让它工作。我对GCD不太熟悉,但是我试着按照我发布的代码的完成代码块的内容进行操作,并在单元被点击时挂起应用程序。不过谢谢 – mike