4

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

4

2 回答 2

1

试试这个:

[self performSelectorOnMainThread:@selector(updateBar:) withObject:[NSNumber numberWithFloat:((float)totalBytesReadForFile/(float)totalBytesExpectedToReadForFile)] waitUntilDone:YES];
于 2013-01-27T03:46:00.317 回答
0

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

于 2013-01-27T05:54:13.370 回答