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

时间:2013-01-26 22:22:58

标签: iphone ios uicollectionview uicollectionviewcell uiprogressbar

我疯了,我正在尝试更新UICollectionViewCelll的进度栏,当我下载文件时,我已经尝试了一切,所有的一切,这是我的最后一次尝试,我有创建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工作,进度条没有...请帮我更新这个进度条...

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:0)

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