我正在嘗試構建一個應用程序,該應用程序將顯示可以下載並閱讀的雜誌集合。我想要發生的是,如果雜誌沒有下載,點擊單元格時將開始下載,並且在顯示下載進度的相應單元格中將顯示進度視圖。我已分類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