Push segue from UITableViewCell to ViewController in Swift
The following steps should fix your problem. If not, please let me know.
Remove your
tableView(tableView, didSelectRowAtIndexPath:)
implementation.Make
data
onRestaurantViewController
have typeNSDictionary!
Determine the selected row in
prepareForSegue
:override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { if let cell = sender as? UITableViewCell { let i = tableView.indexPathForCell(cell)!.row if segue.identifier == "toRestaurant" { let vc = segue.destinationViewController as RestaurantViewController vc.data = currentResponse[i] as NSDictionary } } }
The problem is that you're not handling your data correctly.
If you look into your currentResponse
Array, you'll see that it holds NSDictionaries but in your prepareForSegue
you try to cast a NSDictionary to a NSArray, which will make the app crash.
Change the data
variable in RestaurantViewController
to a NSDictionary and change your prepareForSegue
to pass a a NSDictionary
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let cell = sender as? UITableViewCell {
let i = redditListTableView.indexPathForCell(cell)!.row
if segue.identifier == "toRestaurant" {
let vc = segue.destinationViewController as RestaurantViewController
vc.data = currentResponse[i] as NSDictionary
}
}
}
For Swift 5
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let cell = sender as? UITableViewCell {
let i = self.tableView.indexPath(for: cell)!.row
if segue.identifier == "toRestaurant" {
let vc = segue.destination as! RestaurantViewController
vc.data = currentResponse[i] as NSDictionary
}
}
}