Search code examples
swiftsegue

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


I have a ViewController that contains a UICollectionView where each cell is a custom cell. I need to perform a segue, so when the user taps over any of the cell, a new ViewController is shown for showing the detail of the pressed cell.

With the code I have right now, the segue is performed if I press over a cell with two fingers, but not with just one. Moreover, when the DetailsViewController is show, the title is not updated.

I cannot post images, but the segue I create in the storyboard goes from the cell to the DetailsViewController, is type Show (e.g. Push) and the id is 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]
    }
}

DetailsViewController.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.
    }

}

Any idea what is going wrong?


Solution

  • Change sender to the collectionView cell:

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

    Use this in prepare for 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]
    }