I want to pass a different url to the next VC compared to which cell the user selected via my prepare segue but how can I do that ? Thanks !
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let idGenderMovie = gendersDataModel[indexPath.row].id
let urlString = "https://api.themoviedb.org/3/discover/movie?api_key=&with_genres=\(idGenderMovie)"
performSegue(withIdentifier: "showMovies", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let showMovieFilteredVC = segue.destination as? ShowFilteredMovie else {return}
showMovieFilteredVC.urlGender = //???
}
As Matt says in his comment, you know which cell was selected in your didSelectRowAt()
method. You extract a URL from your data model using the user's selected row, fetch some data from that URL, and then drop that information on the floor.
Instead, create an instance variable in your view controller, userSelectedRow, and set it in your didSelectRowAt() method. Then, in your prepareForSegue, use the selected row to fetch the info you need and pass it to your destination view controller.
Or, as Matt suggests, don't save anything. Leave the row selected, and in prepare(for:)
, interrogate your table view for its selected row, and use that to fetch your data.
By the way, if the URL is to a remote server, you should not read it using Data(contentsOf:)
. That is a synchronous call that will block your user interface until it completes. You could cause your UI to freeze for up to 2 minutes trying to read data from a remote server with that call. (And that would cause your app to be killed as unresponsive.)