gpt4 book ai didi

iphone - 下载文件时如何更新 UICollectionViewCell 子类中的进度条

转载 作者:可可西里 更新时间:2023-11-01 06:10:50 24 4
gpt4 key购买 nike

我要疯了,我正在尝试更新 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 完美运行,我可以看到我下载的所有字节,但进度条不会自行更新,始终保持为零...我不明白该怎么做...这就是我的方式显示单元格,并调用下载方法:

- (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工作,进度条不...请帮我更新这个进度条...

最佳答案

试试这个:

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

关于iphone - 下载文件时如何更新 UICollectionViewCell 子类中的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14542188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com