4

私は夢中になってUICollectionViewCelllいます、私はファイルをダウンロードするときに a のプログレスバーを更新しようとしています、私はすべてを試しました、すべてすべて、これが私の最後の試みです、私はファイル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 の場合は問題ありません。私のコードをもう一度確認してください: https://github.com/lequysang/TestCollectionViewWithProgressBar

于 2013-01-27T05:54:13.370 に答える