Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didSelectItemAt not being called

I have my collection view ready to go and I'm trying to do didSelectItemAt 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() {
        super.viewDidLoad()
        collection.dataSource = self
        collection.delegate = self
        searchBar.delegate = self

        activityIndicatorView.isHidden = true


        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
        view.addGestureRecognizer(tap)
    }

*

What am I doing wrong?

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

    movie = MOVIE_ARRAY[indexPath.row]
    print(movie.plot)
}

enter image description here

like image 775
Ben Wong Avatar asked Sep 11 '25 03:09

Ben Wong


1 Answers

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
view.addGestureRecognizer(tap)
like image 191
Sam_M Avatar answered Sep 12 '25 17:09

Sam_M