更新UICollectionViewCell中的UIProgressView以獲取下載文件

[英]Update UIProgressView in UICollectionViewCell for download file


I'm trying to update the UIProgressView in UICollectionViewCell when I download a file, but sometime the progressView update and sometime doesn't update, and I can't understand why, this is the code to display the UICollectionViewCell:

當我下載一個文件時,我試圖更新UICollectionViewCell中的UIProgressView,但有時progressView更新,有時不更新,我不明白為什么,這是顯示uicollviewcell的代碼:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"documentCell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

NSManagedObject *doc = [self.magDocument objectAtIndex:indexPath.row];

 UIButton *btn = (UIButton *)[cell viewWithTag:2003];
[btn addTarget:self action:@selector(addDocument:) forControlEvents:UIControlEventTouchUpInside];
UIProgressView *progressBar = (UIProgressView *)[cell viewWithTag:2005];

return cell;
}

This is the button to start the download:

這是啟動下載的按鈕:

- (IBAction)addDocument:(id)sender
{
self.directCellPath = [self.myCollectionView indexPathForCell:(UICollectionViewCell *)[[sender superview] superview]];

NSManagedObject *document = [self.magDocument objectAtIndex:self.directCellPath.row];

[self loadLink:[document valueForKey:@"docUrl"]];
}

- (void)loadLink:(NSString *)urlDownload
{
UICollectionViewCell *cell = (UICollectionViewCell *)[self.myCollectionView cellForItemAtIndexPath:self.directCellPath];
UIProgressView *prg = (UIProgressView *)[cell viewWithTag:2005];

AFDownloadRequestOperation *request = [[AFDownloadRequestOperation alloc] initWithRequest:requestUrl targetPath:zipDownloadPath shouldResume:YES];

    [request setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {

            NSLog(@"%f",totalBytesReadForFile/(float)totalBytesExpectedToReadForFile);
        NSArray *progress = [NSArray arrayWithObjects:prg,[NSNumber numberWithFloat:totalBytesReadForFile/(float)totalBytesExpectedToReadForFile], nil];

        [self performSelectorOnMainThread:@selector(updateProgressBar:) withObject:progress waitUntilDone:NO];
    }];

    [self.downloadQueue addOperation:request];
 }

- (void)updateProgressBar:(NSArray *)progress
{
UIProgressView *pgr = (UIProgressView *)[progress objectAtIndex:0];
[pgr setProgress:[[progress objectAtIndex:1] floatValue]];
}

One time the the progress view works, and other one thousand times doesn't work, I can't understand how update the progress view, any help?

一次進度視圖工作,另一次一千次不工作,我不明白如何更新進度視圖,有什么幫助嗎?

2 个解决方案

#1


2  

Create your customCell and display all Visible UIViews in it(init , add subview...). Than use custom method in cellForItemAtIndexPath to active(display) it. If you think about MVC, cellForItemAtIndexPath just for Controller, not View.

創建你的customCell並在其中顯示所有可見的uiview (init, add subview…)。在cellForItemAtIndexPath中使用自定義方法來激活(顯示)它。如果你考慮MVC, cellForItemAtIndexPath只是用於控制器,而不是視圖。

In your case, bring all IBAction, loadLink and updateProgress to customCell and send parameter to active them, or you can create new protocol like CustomCellDelegate to communicate.

在您的示例中,將所有IBAction、loadLink和updateProgress帶到customCell並將參數發送給活動的它們,或者您可以創建像CustomCellDelegate這樣的新協議來進行通信。

Check for more information:

檢查的更多信息:

Error setting text in collection view cell

在集合視圖單元格中設置文本錯誤

#2


0  

It might be about the thread issue. just try

這可能與線程問題有關。試試

dispatch_async(dispatch_get_main_queue(), ^{
            UIProgressView *pgr = (UIProgressView *)[progress objectAtIndex:0];
            [pgr setProgress:[[progress objectAtIndex:1] floatValue]];
       });//end block

try async or sync

異步或同步


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/01/07/720cf43a995c1ab68ef3d28b03784be3.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com