问题
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;
}
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?
回答1:
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.
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.
Check for more information:
Error setting text in collection view cell
回答2:
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://stackoverflow.com/questions/14204204/update-uiprogressview-in-uicollectionviewcell-for-download-file