How to update progressBar in subclass of UICollectionViewCell when loading a file

I'm going crazy, I'm trying to update the progressBar from UICollectionViewCelll , when I upload a file, I tried everything, everything, this is my last attempt, I created a subclass of UICollectionViewCell associated with the xib file:

 #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 works fine, I can see all the bytes that I download, but progressBar does not update itself, it always remains at zero ... I can’t figure out how to do this ... and this is how I display the cell and call the method to load :

 - (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; } 

Can anybody help me? the file upload method is called, the nslog of the byte is loaded, the progress bar does not work ... please help me update this progress bar ...

+4
source share
2 answers

This is my first experience with UIProgressView. I have the same problem when trying to set a UIProgressView frame. But when initWithProgressViewStyle is normal. Check my code again: https://github.com/lequysang/TestCollectionViewWithProgressBar

0
source

try the following:

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

All Articles