0

我正在尝试构建一个应用程序,该应用程序将显示一系列可以下载然后阅读的杂志。我想要发生的是,如果没有下载杂志,当点击一个单元格时将开始下载,并且相应的单元格中将出现一个进度视图,显示下载进度。我已经子类化UICollectionViewCell并添加了一个UIProgressView属性。到目前为止,我已经能够检查文件是否已下载,如果没有,请下载它,如果是,只需使用PDF library called VFR Library. 我还可以更新放置在 之外的进度视图,UICollectionViewCell但无法更新放置在UICollectionViewCell. 不幸的是,我下载和更新进度视图的大部分逻辑都在didSelectItemAtIndexPath:. 我知道这并不优雅,但我的经验水平还不够高,无法有效地开发自己的课程以无缝协作。但是一旦我弄清楚了这一点,我将致力于改进代码并抽象一些东西。无论如何,这就是我正在做的didSelectItemAtIndexPath:

- (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];

    }



 }

那么有什么方法可以更新此方法中相应单元格内的进度视图,还是我需要做其他事情才能使其正常工作?谢谢你提供的所有帮助。

4

2 回答 2

3

好吧,我终于解决了我自己的问题。显然问题是由于没有完全理解如何在 UICollectionView 中引用特定项目。我所要做的就是创建一个实际执行此操作的方法,然后在上面的“setDownloadProgressBlock:”方法中调用它。我为此写的方法是:

 - (void)setProgressAtIndex:(NSInteger)index withProgress:(float)progress
{
    IssueCell *cell = (IssueCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    [cell.progressView setProgress:0.0];
    [cell.progressView setProgress:progress];
}

此方法中重要的一行是我获取对所需单元格的引用的第一行。这个实际上抓住了被窃听的单元格,而在我想我只是指一般的所有单元格之前?我不太确定,但如果有人有更好的解释,我肯定会很感激。

然后像这样调用此方法:

[self setProgressAtIndex:indexPath.row withProgress:percentDone];

同样,我在“setDownloadProgressBlock:”的块内做了这个。希望这对将来的其他人有所帮助!

于 2013-06-19T15:23:29.647 回答
0

更新主线程上的进度视图。尝试使用dispatch_sync(dispatch_get_main_queue()).

于 2013-05-20T15:46:04.193 回答