2013-05-20 53 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:」。 希望這可以幫助未來的其他人!