下载文件时如何更新UICollectionViewCell子类中的progressBar [英] How update progressBar in UICollectionViewCell subclass when download file

查看:131
本文介绍了下载文件时如何更新UICollectionViewCell子类中的progressBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我快疯了,我正在下载文件时尝试更新UICollectionViewCelll的progressBar,我已经尝试了所有方法,这是我的最后一次尝试,我创建了,并与xib文件连接:

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

NSLog setProgressiveDownloadProgressBlock可以完美地工作,我可以看到我下载的所有字节,但是progressBar不会自我更新,始终保持为零...我不明白该怎么做...这就是我的方式显示单元格,然后调用下载方法:

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

有人可以帮助我吗?称为下载文件的方法,下载字节的nslog起作用,进度条不...请帮助我更新此进度条...

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...

推荐答案

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

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

这篇关于下载文件时如何更新UICollectionViewCell子类中的progressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆