DidSelectItemAt not called

I have a ready-made collection, and I'm trying to do didSelectItemAt to go to the detailed view. But I just want to check the registration of each of the elements and not register.

I already installed all the delegates:

*

 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

+5
source share
1 answer

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

- var cancelsTouchesInView: Bool {get set}

Boolean that affects whether touches are transmitted to the view when a gesture is recognized.

This is true by default and will prevent the call to didSelectItemAt, since touches will not be delivered to the view after the recognition of the tap. You need to set it to false as follows:

 let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard") tap.cancelsTouchesInView = false view.addGestureRecognizer(tap) 
+18
source

Source: https://habr.com/ru/post/1257474/


All Articles