1

Trying to register a tap on the collection view to dismiss the keyboard using the following

   override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    inputTextField.endEditing(true)

}

however I'm getting a compile error saying that the method doesn't override any method from it's superclass.

The class is of type UICollectionViewController and UICollectionViewDelegateFlowLayout

Also using swift 3.

Thanks.

sschale
  • 5,168
  • 3
  • 29
  • 36
Stefan
  • 908
  • 1
  • 11
  • 33

2 Answers2

4

That's not a Swift 3 function - you want the updated version of that - you can tell because its first parameter doesn't have a _ in front. The one you actually want is:

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

  }

Use that one instead and everything should work.

sschale
  • 5,168
  • 3
  • 29
  • 36
  • It is not easy for me to discover this issue, haha. – chengsam Mar 13 '17 at 05:10
  • @chengsam It bit me really, really hard when converting a large project from Swift 2 -> 3 – sschale Mar 13 '17 at 05:10
  • I've been learning swift for a while and just got around to updating to swift 3. Btw, solution works perfectly. Thanks. – Stefan Mar 13 '17 at 05:16
  • Hi @sschale, could you explain what this underscore is? I'm completely new to Swift and I haven't done any UI development in a long time. This construct is confusing me; are these callbacks on the CollectionView class, or are we overriding method overloads on the base class? – rbrtl Sep 02 '18 at 12:46
  • For anyone swinging by here, I answered my own question with 1 Google search (yeah, I know): https://stackoverflow.com/questions/39627106/why-do-i-need-underscores-in-swift – rbrtl Sep 02 '18 at 12:48
-1

Just remove the override keyword.

chengsam
  • 7,315
  • 6
  • 30
  • 38
  • Removing the override removes the error but doesn't register the tap on the collection view. – Stefan Mar 13 '17 at 04:44
  • Did you class implements `UICollectionViewDelegateFlowLayout`? And make sure your collectionView has set the delegate. – chengsam Mar 13 '17 at 04:47
  • I used sizeForItemAtIndexPath perfectly fine so I know UICollectionViewDelegateFlowLayout is implemented and the delegate is set. Is there new syntax for didSelectItemAtIndexPath? – Stefan Mar 13 '17 at 05:07
  • sschale's answer should be your solution. – chengsam Mar 13 '17 at 05:09