ios – Swift can’t segue data from CollectionView to new ViewController


In several places in my app I segue data from a TableView to the following ViewController and it works as simply as this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        let indexpath = tableView.indexPathForSelectedRow
        if segue.destination is NewViewController {

                        let name = self.people[indexpath!.row].name
                        let description = self.people[indexpath!.row].desc
                        let vc = segue.destination as? NewViewController
                        vc?.username = name

However this doesn’t work at all, for some reason, with a CollectionView (though I feel it absolutely should.)

Value of type 'UICollectionView' has no member 'indexPathForSelectedRow'

I’ve also tried most of the suggestions on StackOverflow such as:

   let selectedIndexPath = sender as? IndexPath
                let vc = segue.destination as! PlacesViewController
        vc.named = self.places[selectedIndexPath!.row].name as String

But all it’s gotten me is a nil value.

Why does it not simply work the same way that a tableView would work? (As you’d expect it to in Objective-C… or even as it works with RecyclerView and Gridview in Android…)