didSelectItem没有被调用

时间:2016-09-29 21:32:52

标签: ios swift uicollectionview

我准备好了我的集合视图,我正在尝试didSelectItemAt来查看详细信息视图。但我只想测试记录每个项目,而不是记录。

我已经设置了所有代表:

*

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

*

我做错了什么?

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

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

enter image description here

1 个答案:

答案 0 :(得分:47)

您在视图中添加了TapGestureRecognizer。 TapGestureRecognizer具有属性cancelsTouchesInView

  

- var cancelsTouchesInView:Bool {get set}

     

一个布尔值,用于影响在识别手势时是否将触摸传递到视图。

默认情况下为true,并且会阻止调用didSelectItemAt,因为在识别到点击后,触摸将不会传递到视图。 您需要将其设置为false,如下所示:

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