How update progressBar in UICollectionViewCell sub

2019-06-27 01:05发布

问题:

I'm going crazy, i'm trying to update the progressBar of a UICollectionViewCelll when i download a file, i have tried everything, everything everything, this is my last attempt, i have create a subclass of UICollectionViewCell, connected with a 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]];
}

the NSLog setProgressiveDownloadProgressBlock works perfectly, i can see all the byte i download, but the progressBar don't update itself, remain always at zero...i can't understand how do it...and this is how i display the cell, and call the method to download:

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

anyone can help me? the method to download the file is called, the nslog of the byte downloaded works, the progress bar don't...please help me to update this progressbar...

回答1:

This is my first time working with UIProgressView. I have same problem when try to set UIProgressView frame. But when initWithProgressViewStyle, It's OK. Please check my code again: https://github.com/lequysang/TestCollectionViewWithProgressBar



回答2:

try this:

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