3

当我下载文件时,我试图更新 UICollectionViewCell 中的 UIProgressView,但有时 progressView 更新,有时不更新,我不明白为什么,这是显示 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]];
}

一次进度视图有效,另外一千次无效,我不明白如何更新进度视图,有什么帮助吗?

4

2 回答 2

2

创建您的 customCell 并在其中显示所有 Visible UIViews(初始化,添加子视图...)。比在 cellForItemAtIndexPath 中使用自定义方法来激活(显示)它。如果您考虑 MVC,cellForItemAtIndexPath 仅用于控制器,而不是视图。

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

查看更多信息:

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

于 2013-01-13T06:58:36.393 回答
0

可能是线程问题。试试看嘛

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

尝试异步或同步

于 2013-01-07T21:36:38.260 回答