Home > DeveloperSection > Forums > Getting cell index number of Collection view from out side the delegate methods in iOS.
Tarun Kumar

Total Post:210

Points:1614
Posted on    January-01-2016 7:26 AM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 467  View(s)
Rate this:

I want to find the touched cell index number in collection view controller from out side its delegate methods.  I want to create a method which helps me to find out the index no. but my problem is how can I find the cell index.

If is this possible then please provide me the code also.

Thank you.


Pooja Malohtra
Pooja Malohtra

Total Post:47

Points:331
Supported
Posted on    January-01-2016 11:23 PM

Yes, you can get the cell index no. with the help of UITapGestureRecognizer. You need to add the gesture recognizer on the cell view. Use gesture recognizer like this:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didImageSelect:)];
recognizer.delegate = self;
recognizer.numberOfTapsRequired=1;
[cellImage addGestureRecognizer:recognizer];

above code we are adding gesture recognizer on cellImage reference. here cellImage is the reference of UIImageView which will be added on the cell view. and also we are using didImageSelect: method on the @selector.
now, here is the @selector didImageSelect: method code which will be responsible for getting cell index:

-(void)didImageSelect:(UITapGestureRecognizer*)recognizer{
  CGPoint *point = [recognizer locationInView:_collectionView];
  NSIndexPath *indexPath = [_collectionView indexPathForItemAtPoint:point];
  NSLog(@"IndexPath:%d",[indexPath row]);
}

now, we get the cell index on the click on any cell in collection view  on the indexPath variable.


Don't want to miss updates? Please click the below button!

Follow MindStick