在UICollectionViewCell中更新UIProgressView

时间:2013-05-20 15:35:09

标签: ios objective-c uicollectionview uiprogressview

我正在尝试构建一个应用程序,该应用程序将显示可以下载然后阅读的杂志集合。我想要发生的是如果未下载该杂志,则在点击一个单元格时将开始下载,并且相应的单元格中将显示进度视图以显示下载进度。我已将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];

    }



 }

那么有什么方法可以在这个方法中的相应单元格内更新进度视图,或者我还需要做些什么才能让它工作?谢谢你提供的所有帮助。

2 个答案:

答案 0 :(得分: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:”的块中做了这个。 希望这对未来的其他人有所帮助!

答案 1 :(得分:0)

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