2013-05-20 51 views
0

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

    } 



} 

我这样做是没有办法,我可以在这个方法中更新相应的单元格内的进步观点还是有别的东西,我需要做的就是它的工作任何方式?谢谢你提供的所有帮助。

回答

0

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

+0

感谢您的回复。不幸的是,我无法让它工作。我对GCD不太熟悉,但是我试着按照我发布的代码的完成代码块的内容进行操作,并在单元被点击时挂起应用程序。不过谢谢 – mike

2

那么,我终于解决了我自己的问题。显然问题是由于不完全理解如何引用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:”。 希望这可以帮助未来的其他人!