Olyve
Olyve

Reputation: 731

DidSelectItemAtIndexPath not working with collectionview

So I have been struggling with this for a while now. I have a UICollectionView that I am using as a menu. The cells are the options to switch to another page. The menu functions exactly how it should except that when you press a cell, say cell 0, it should pop to the next view. What I am finding is that the cell is registering the touch but when I try and determine which cell was pressed is when it falls apart. I have tried debugging it and to me it looks like indexPath has no value! I am using the didSelectItemAtIndexPath function, no it is not didDeselect (I checked that already from my searches on how to fix this). I will post the code but this one has really stumped me. Any help would be greatly appreciated!

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
    NSLog("Pressed Cell")

    if(indexPath == 0)
    {
        self.navigationController?.popToViewController(profileViewController, animated: true)
    }

}

Upvotes: 0

Views: 3241

Answers (2)

Imran Khan
Imran Khan

Reputation: 9

I am sure normally call the delegate method but if have add any Gesture to the view. Make sure before you add your gesture recognizers to you're view, make sure the boolean property "cancelTouchesInView" is set to false. This could be because the gesture is recognized so it ignores passing touches to the view causing the cell selection method not to be called. Link to property.

Upvotes: 0

pbasdf
pbasdf

Reputation: 21536

NSIndexPath comprises both a section and an item, which you can access as indexPath.item and indexPath.section. Assuming you have only one section (so its value is irrelevant), you can change your code to:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
    NSLog("Pressed Cell")

    if(indexPath.item == 0)
    {
        self.navigationController?.popToViewController(profileViewController, animated: true)
    }

}

Upvotes: 1

Related Questions