下载文件时如何在UICollectionViewCell子类中更新progressBar

我要疯了,我试图更新UICollectionViewCelll当我下载一个文件,我已经尝试了一切,一切的一切,这是我最后一次尝试,我已经创build了一个UICollectionViewCell的子类,与xib文件连接:

 #import <UIKit/UIKit.h> @interface DocumentCell : UICollectionViewCell @property (weak, nonatomic) IBOutlet UIImageView *documentImage; @property (weak, nonatomic) IBOutlet UIProgressView *downloadBar; - (void)downloadDocumentWithUrl:(NSURLRequest *)requestUrl; @end - (void)downloadDocumentWithUrl:(NSURLRequest *)requestUrl { ..... AFDownloadRequestOperation *request = [[AFDownloadRequestOperation alloc] initWithRequest:requestUrl targetPath:zipDownloadPath shouldResume:YES]; [request setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Successfully downloaded file to %@", zipDownloadPath); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; [request setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) { NSLog(@"%f",totalBytesReadForFile/(float)totalBytesExpectedToReadForFile); [self performSelectorOnMainThread:@selector(updateBar:) withObject:[NSNumber numberWithFloat:(totalBytesReadForFile/(float)totalBytesExpectedToReadForFile)] waitUntilDone:YES]; }]; [downloadQueue addOperation:request]; } - (void)updateBar:(NSNumber *)floatNumber { [self.downloadBar setProgress:[floatNumber floatValue]]; } 

NSLog setProgressiveDownloadProgressBlock完美的作品,我可以看到所有的字节我下载,但progressBar不更新自己,始终保持在零…我不明白怎么做…这是我如何显示单元格,并调用下载的方法:

 - (void)startDownload:(NSString *)downloadUrl { //DocumentCell *cell = (DocumentCell *)[self.collectionView cellForItemAtIndexPath:self.downloadPath]; i have tried also in this way DocumentCell *cell = (DocumentCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:self.downloadPath]; [cell downloadDocumentWithUrl:requestUrl]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. UINib *cellNib = [UINib nibWithNibName:@"DocumentCell" bundle:nil]; [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:CellIdentifier]; ... } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { DocumentCell *cell = (DocumentCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; cell.documentImage.....etc return cell; } 

任何人都可以帮助我? 调用下载文件的方法,下载的字节nslog,进度条不…请帮助我更新这个进度条…

这是我第一次使用UIProgressView。 尝试设置UIProgressView框架时遇到同样的问题。 但是,当initWithProgressViewStyle,没关系。 请再次检查我的代码: https : //github.com/lequysang/TestCollectionViewWithProgressBar

尝试这个:

 [self performSelectorOnMainThread:@selector(updateBar:) withObject:[NSNumber numberWithFloat:((float)totalBytesReadForFile/(float)totalBytesExpectedToReadForFile)] waitUntilDone:YES];