Question

I want to display progress view in collection view when user click on the cell and also want to display progress of that cell's progress view.

//...in .h file i have declared UIProgressView -

@property(nonatomic,retain)IBOutlet UIProgressView *pv;

//...then use it in .m file as -

I have added UIProgressView in UICollectionView's method cellForItemAtIndexPath as follows;

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"ArticleCell" forIndexPath:indexPath];

    pv = [[UIProgressView alloc]init];
    pv.frame = CGRectMake(45, 270, 230, 50);
    pv.progress = 0.0f;
    pv.progressViewStyle = UIProgressViewStyleDefault;
    pv.hidden = YES;

   [cell.contentView addSubview:pv];

  return cell;

}

And then show progress view and its progress in didSelectItemAtIndexPath as follows;

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
 //to show progressview in cell..
            UICollectionViewCell *selectedCell = [collView cellForItemAtIndexPath:indexPath];

            NSLog(@"subviews - %@",selectedCell.contentView.subviews);
            pv = (UIProgressView *)[selectedCell.contentView.subviews objectAtIndex:0];
            pv.hidden = NO;

           [self performSelector:@selector(checkDownload) withObject:nil afterDelay:1.0];
}


//to check whether all images are downloaded or not and showing progress accordingly
-(void)checkDownload
{
    NSError *error;

    NSArray* arrArticleImages = [[NSArray alloc]init];

    arrArticleImages = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:imgPath error:&error];
    NSLog(@"arr cnt-%d",arrArticleImages.count );

    if (arrArticleImages.count <= 8)
    {
        float m = (float)arrArticleImages.count / 100;
        float t = pv.progress ;
        float g = m + t;

        pv.progress = g;

        NSLog(@"prgs-%f",g);

        [self performSelector:@selector(checkDownload) withObject:nil afterDelay:4.0];
    }
    else
    {
        pv.progress = 1.0;

        [self insertIntoDatabase];

        [self performSelector:@selector(goToNewsPaperView) withObject:nil afterDelay:2.0];
    }

    arrArticleImages = nil;
}

But now problem with this is that, i click on one cell it starts showing progressview with progress but as soon as click on another cell in between , the first cell's progress stops showing progress and second progress starts showing progress.

I want to display each cell's progressview's progress if it is clicked.

I think my approach is in wrong way.

I can not be able to find it out.

How can i do this??

Please help me.

Thanks..

Was it helpful?

Solution

It is because yout getting new refrence of your progress view at didSelectItemAtIndexPath. what about previous refrence of progrress view.

You can add all progress view in NSDictionary with keyvalue index path. And in checkDownload pass your index path then get that progress view from NSDictionary with passed index path and use it

//Initialize you NSMutable dictionary somewhere else And add progress view either in

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
        {
            UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"ArticleCell" forIndexPath:indexPath];

            pv = [[UIProgressView alloc]init];

            [dictionary setObject:pv forKey:indexpath];

        }

Or you can add progress view in

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
        {
         //to show progressview in cell..
             UICollectionViewCell *selectedCell = [collView cellForItemAtIndexPath:indexPath];

             NSLog(@"subviews - %@",selectedCell.contentView.subviews);
             pv = (UIProgressView *)[selectedCell.contentView.subviews objectAtIndex:0];
             pv.hidden = NO;
             [dictionary setObject:pv forKey:indexpath];

             [self performSelector:@selector(checkDownload) withObject:indexpath afterDelay:1.0];
        }

Customize you checkDownload with

  -(void)checkDownload:(NSIndexPath*)indexpath
    {
       pv = [dictionary objectForKey:indexpath];
    }

OTHER TIPS

This is because you are using the same object pv in all cells, you need to do the following:

Create UIProgressView for each cell:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"ArticleCell" forIndexPath:indexPath];

    UIProgressView *pv = [[UIProgressView alloc] init];
    pv.tag = 10;
    .....
}

And in your didSelectItemAtIndexPath get the reference and pass it to the download function:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
 //to show progressview in cell..
        UICollectionViewCell *selectedCell = [collView cellForItemAtIndexPath:indexPath];

        NSLog(@"subviews - %@",selectedCell.contentView.subviews);
        UIProgressView *pv = (UIProgressView *)[selectedCell viewForTag:10]; 

       // pass the progressView to your checkDownload
       [self performSelector:@selector(checkDownload:) withObject:pv afterDelay:1.0];
}

and the checkDownload will be:

-(void)checkDownload:(UIProgressView *)progressView{
   //use progressView instead of pv
   ....
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top