1

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..

Rohan
  • 2,939
  • 5
  • 36
  • 65

2 Answers2

2

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];
    }
Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29
  • Thanks for your reply. Exactly i want like this. But i have no idea how to do this. Can you please explain it in brief or provide sample code?? – Rohan Nov 21 '13 at 05:51
  • 1
    You can take dictionary as instance variable add progress view to that dictioanty in cellForIndexPath or didSelectItemAtIndexPath method respective of index path – Bhumeshwer katre Nov 21 '13 at 05:54
  • thanks bro. your answer is the exact solution. but i can not be able to test it. because i am checking image downloading in **-(void)checkDownload:(NSIndexPath*)indexpath** method. and as i clicked in 2nd cell , the path is chenged and pv.progress doesnt work. So need to call this method in queue. how can i do this? – Rohan Nov 21 '13 at 09:09
  • 1
    You can do this with NSOperation. This link may help you "http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues" – Bhumeshwer katre Nov 21 '13 at 09:31
0

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
   ....
}
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68