Oli Black
Oli Black

Reputation: 441

Get indexPath of UICollectionViewCell

I have a custom CollectionViewCell class, I have then generated an amount of these inside of a CollectionView. I have then also added a modal segue when the ViewCells are touched. How would I get the id (or index path) of which cell was pressed when the modal segue is activated.

I saw on here that someone suggested I add into the ViewCell.m file the method

-(void)collectionView: (UICollectionView*)collectionView didSelectItemAtIndexPath:   (NSIndexPath*)indexPath{
//get indexpath variable in here
}

But this method does not seem to be getting called. Can anyone help me with what method would need to be get called for when a CollectionViewCell has a modal segue to get the indexpath.

Thanks,

Upvotes: 1

Views: 2782

Answers (2)

ryan cumley
ryan cumley

Reputation: 1931

If the views you have added to your cells are handling the touch by firing a segue, then the collectionViewCell never has a chance to receive the touch.

If you want the collectionViewCell to handle it, make the subviews of your cell purely decorative by disabling their behavior to fire the segue. (for example, if they are buttons and "touchUpInside" fires the segue, then change them into views with no touch handling at all.).

Then the touch will be passed up through the view hierarchy until the CollectionViewCell handles it, which it does with the collectionView:didSelectItemAtIndexPath: method.

Upvotes: 1

JeroValli
JeroValli

Reputation: 569

The method is a callback of the delegate class of the collection view object:

-(void)collectionView: (UICollectionView*)collectionView didSelectItemAtIndexPath:   (NSIndexPath*)indexPath

So you should implement on the .m file of the delegate class of you collection view.

Upvotes: 0

Related Questions