Search code examples
iosswiftswift4collectionviewswift5

collectionView didSelectItemAtIndexPath stopped working after migrated to Xcode 10.2


I just upgraded my Xcode to version 10.2, after I installed the latest Mojave macOS.

Xcode asked me to migrate my Swift from 3.0 to later versions, so I chose to update to Swift 5.0.

After a few minor changes regarding grammar change, the app is up and running. However, I found that collectionView didSelectItemAtIndexPath stopped working in the simulator or device, which means when you tap on an item in the collectionView, the corresponding didSelectItemAtIndexPath method is not longer being called.

I examined my code changes many times, I am pretty sure the grammar changes are unrelated to the collectionViews being affected. I can make some TagGesture tricks to work around, but I wonder what's the problem here.

I am pretty sure the collection view allows user interaction, and allows selection too.

What's going on here?

update:

My old code has no changes at all after the migration, it looks like this:

func collectionView(_ collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: IndexPath) {
...
}

I updated to the following per answerers' suggestion. However, still not working.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
...
}

(I didn't even touch the xib file too, the delegates are all correct.)


Solution

  • The delegate method was probably renamed. Check that you're implementing:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    

    Also double-check that you're still setting the controller as a delegate for the UICollectionView.