ios - 在 UICollectionViewCell 中更新 UIProgressView

标签 ios objective-c uicollectionview uiprogressview

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

    }



 }

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

最佳答案

好吧,我终于解决了自己的问题。显然问题是由于没有完全理解如何在 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:”中的 block 内进行了此操作。 希望这对将来的其他人有帮助!

关于ios - 在 UICollectionViewCell 中更新 UIProgressView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16653026/

相关文章:

ios - 在应用于单元格之前未设置自定义 Collection View 布局属性

ios - Swift POST 中的授权 header

ios - 在 App Store 上发布对现有应用程序的更新时签署 iOS 应用程序

iphone - 如何在iOS中用颜色遮盖图像?

ios - 添加 subview Controller 并更改其标签

ios - 如何找出哪个类(class)插入了我?

android - Visual Studio 2015 移动跨平台

iphone - 限制可用的本地化

ios - 仪器和泄漏

ios6 - 如何强制使用较少项目的 UICollectionView 滚动?