didSelectItemAt not being called

◇◆丶佛笑我妖孽 提交于 2020-06-24 07:11:10

问题


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)
}


回答1:


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)


来源:https://stackoverflow.com/questions/39780373/didselectitemat-not-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!