Ben Wong Ben Wong - 1 year ago 370
Swift Question

didSelectItemAt not being called

I have my collection view ready to go and I'm trying to do

to segue to the detail view. But I just want to test out logging each of the items and it's not logging.

I set all the delegates already:


class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UISearchBarDelegate {*

@IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var collection: UICollectionView!

override func viewDidLoad() {
collection.dataSource = self
collection.delegate = self
searchBar.delegate = self

activityIndicatorView.isHidden = true

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")


What am I doing wrong?

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

movie = MOVIE_ARRAY[indexPath.row]

enter image description here

Answer Source

You have added a TapGestureRecognizer on the view. TapGestureRecognizer has a property cancelsTouchesInView.

- var cancelsTouchesInView: Bool { get set }

A Boolean value affecting whether touches are delivered to a view when a gesture is recognized.

This is true by default and will prevent calling didSelectItemAt since touches will not be delivered to the view after a tap is recognized. You need to set it to false like this:

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
tap.cancelsTouchesInView = false