3

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?

Dmitry
  • 279
  • 2
  • 10
Piero
  • 9,173
  • 18
  • 90
  • 160

2 Answers2

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.

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

Community
  • 1
  • 1
LE SANG
  • 10,955
  • 7
  • 59
  • 78
  • thank you very much for the answer, first i'll accept your answer, can you create a sample code in which explain the solution, a simple uicollection view and uicollectioviewcell subclass, and insert a uiimageview that load an image from an url and display it in the uicollectionviewcell, so i can see how use this class, i will appreciate very much if you do this sample code :( thanks – Piero Jan 13 '13 at 13:35
  • thank you very much! so i have to add the download code inside the uicollectionviewcell? – Piero Jan 14 '13 at 12:44
  • and if i want a class that handle all the download, to handle a queue, how i can then send the progress bar update to the uicollectionviewcell subclass? – Piero Jan 14 '13 at 12:45
  • hello, @Sang take a look at this question, http://stackoverflow.com/questions/14542188/how-update-progressbar-in-uicollectionviewcell-subclass-when-download-file i have write the code for your request, and doesn't work :( help! – Piero Jan 27 '13 at 00:03
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

Güngör Basa
  • 628
  • 2
  • 9
  • 27