0

I have a dictionary with the same keys:

var food = [["item" : "Burger", "image": "burger"],
            ["item" : "Pizza", "image": "pizza"],
            ["item" : "Lunch", "image": "lunch"],
            ["item" : "Coffee", "image": "coffee"]],

And it is used in my table view. Each cell dispalys a name.

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell

    let foodObjects = food[indexPath.row]

    cell.background.image = UIImage(named: foodObjects["image"]!)
    cell.nameLabel.text  = foodObjects["item"]

    return cell
}

And what I want, is item value from selected cell be passed to second View Controller. I know that I can do this throughout this function, but I cannot get a value from selected item. Could somebody help me?

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let indexPath = self.tableView.indexPathForSelectedRow?.row

    let resVC = segue.destination as! RestaurantVC //second View controller

    let selectedItem = food[indexPath.["item"]] // How to do this??
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
belab
  • 117
  • 3
  • 12

6 Answers6

2

Create property like

var selectedItem on your controller.

On did select save that item to selectediItem.

Than use it in prepare segue method.

Vishal
  • 139
  • 4
1

use this method to select a specific object from the tableview

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

 //print(food[indexPath.row].first?.value)
            }
Anil Kumar
  • 945
  • 7
  • 16
1

If you want to find the selected indexpath from override func prepare(for segue: UIStoryboardSegue, sender: Any?) function. you can use tableView.indexPath(for cell: UITableViewCell). Try the following code

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

        let destinationViewController = segue.destination as! RestaurantVC

        if let selectedCell = sender as? UITableViewCell
        {
            let indexPath = tableView.indexPath(for: selectedCell)
            let foodObjects = food[indexPath.row]

        }

}
Prashanth Rajagopalan
  • 718
  • 1
  • 10
  • 27
1

Create a property in RestaurantVC like this

var selectedItem:String!

then try this

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let indexPath = self.tableView.indexPathForSelectedRow?.row

    let resVC = segue.destination as! RestaurantVC //second View controller

    resVC.selectedItem = food[tableview.indexPathForSelectedRow.row]["item"] // How to do this??
}
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
  • Something like this works: `let selectedItem = food[indexPath!]["item"]` And when I print it, it's correct selected item. But when I try assign it to label in new viewController, it crash: `resVC.selectedItem.text = selectedItem` **error: fatal error: unexpectedly found nil while unwrapping an Optional value** – belab May 03 '17 at 10:34
  • @belab try printing selected Item in prepare for segue method. – RajeshKumar R May 03 '17 at 10:37
  • I did it, and it's not optional, or nil. Correc value, for example: _Pizza_ – belab May 03 '17 at 10:39
  • Have you declared string property in second view controller? – RajeshKumar R May 03 '17 at 10:41
  • Yes. `@IBOutlet var selectedItem: UILabel!` Maybe it is about Delegates? or It has no matter? – belab May 03 '17 at 10:47
  • No. It is an IBOutlet for a label. You need to create a property like this in second view controller `var selectedItem:String!` – RajeshKumar R May 03 '17 at 10:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143272/discussion-between-rajeshkumar-r-and-belab). – RajeshKumar R May 03 '17 at 10:59
1

Please try this

var food: [Dictionary<String, String>] = [["item" : "Burger", "image": "burger"],
            ["item" : "Pizza", "image": "pizza"],
            ["item" : "Lunch", "image": "lunch"],
            ["item" : "Coffee", "image": "coffee"]]

and you can set selectedItem in controller resVC by following code

resVC.selectedItem = food[indexPath]["item"]!

or

let selectedItem = food[indexPath]["item"]
if let selectedItem = selectedItem {
    resVC.selectedItem = selectedItem
}
Sree
  • 530
  • 1
  • 8
  • 20
1

A possiblity to check out my answer here. Use delegate to retrieve the data from selected cell (which you have passed to cell on cellForRowAtIndexPath like cell.nameLabel.text = foodObjects["item"]) and in the delegate event perform segue to move to new controller and pass the data you received in as a parameter to event. So it will be like:

func foodItemCellSelected(using foodItem: [String]) // Delegate event of custom cell
{
    self.selectedFoodItem = foodItem; // Retrieve selected item and save it in VC
    self.performSegue(.....);  // Perform segue
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let indexPath = self.tableView.indexPathForSelectedRow?.row

    let resVC = segue.destination as! RestaurantVC //second View controller

    resVC.foodItem = self.selectedFoodItem
}
Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56