从 UICollectionViewCell 执行 segue 以显示详细信息屏幕不起作用

Perform a segue from a UICollectionViewCell to show a detail screen not working

我有一个 ViewController,其中包含一个 UICollectionView,其中每个单元格都是一个自定义单元格。我需要执行 segue,因此当用户点击任何单元格时,会显示一个新的 ViewController 以显示按下的单元格的详细信息。

使用我现在拥有的代码,如果我用两根手指而不是一根手指按下一个单元格,就会执行 segue。此外,当显示详细信息ViewController时,标题不会更新。

我不能 post 图片,但我在故事板中创建的 segue 从单元格到详细信息ViewController,类型为 Show(例如 Push),id 为 showDetail。

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.register(UINib.init(nibName: "MovieCell", bundle: nil), forCellWithReuseIdentifier: "MovieCell")


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

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showDetail", sender: indexPath)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if(segue.identifier == "showDetail") {
        guard let viewController = segue.destination as? DetailsViewController else {return}
        guard let indexPath = sender as? IndexPath else {return}
        viewController.movie = self.moviesList[indexPath.row]
    }
}

详情ViewController.swift

class DetailsViewController: UIViewController {


    var movie: Movie? = nil {
        didSet {
            navigationController?.title = movie?.title
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

}

知道出了什么问题吗?

将发件人更改为 collectionView 单元格:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showDetail", sender: MovieCell)
}

使用这个来准备 segue:

if(segue.identifier == "showDetail") {
    guard let viewController = segue.destination as? DetailsViewController else {return}
    let cell = sender as MovieCell
    guard let indexPath = self.collectionView!.indexPathForCell(cell) else {return}
    viewController.movie = self.moviesList[indexPath.row]
}