在UICollectionViewCell中更新UIProgressView以获取下载文件 [英] Update UIProgressView in UICollectionViewCell for download file

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

问题描述

我正在尝试在下载文件时更新UICollectionViewCell中的UIProgressView,但有时progressView更新并且有时不更新,我无法理解为什么,这是显示UICollectionViewCell的代码:

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

这是开始下载的按钮:

- (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?

推荐答案

创建你的customCell并显示所有可见的UIViews它(init,添加子视图......)。比使用cellForItemAtIndexPath中的自定义方法来激活(显示)它。如果你考虑MVC,cellForItemAtIndexPath只适用于Controller,而不是View。

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.

在您的情况下,将所有IBAction,loadLink和updateProgress带到customCell并发送参数以激活它们,或者您可以创建新协议,如CustomCellDelegate进行通信。

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.

检查更多信息:

在集合视图单元格中设置文本时出错

这篇关于在UICollectionViewCell中更新UIProgressView以获取下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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