当我下载文件时,我正在尝试更新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]];
}
一次进度视图有效,而其他一千次无效,我无法理解如何更新进度视图,有什么帮助吗?
答案 0 :(得分:2)
创建customCell并在其中显示所有可见的UIViews(init,add subview ...)。比使用cellForItemAtIndexPath中的自定义方法来激活(显示)它。如果你考虑MVC,cellForItemAtIndexPath只适用于Controller,而不是View。
在您的情况下,将所有IBAction,loadLink和updateProgress带到customCell并发送参数以激活它们,或者您可以创建新协议,如CustomCellDelegate进行通信。
检查更多信息:
答案 1 :(得分:0)
可能是关于线程问题。试试
dispatch_async(dispatch_get_main_queue(), ^{
UIProgressView *pgr = (UIProgressView *)[progress objectAtIndex:0];
[pgr setProgress:[[progress objectAtIndex:1] floatValue]];
});//end block
尝试异步或同步