tableViewController has tableView Cell named MealCell.
The cell has
1. mealImageView(UIImageView)
2. mealNameLabel(UILabel)
3. mealPriceLabel(UILabel)
4. explanation(UITextView)
tableViewController.swift
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPathValue = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPathValue!) as! MealCell
guard let name = currentCell.mealNameLabel?.text else {
return
}
let infoVC = MealInfoViewController()
infoVC.nameOfMeal = name
navigationController?.pushViewController(infoVC, animated: true)
}
To show the values of selected cell on new view, I made new UIViewcontroller on Storyboard and also created file named MealInfoViewController.
Also connect Prototype cell to the UIView.
MealInfoViewController.swift
import UIKit
class MealInfoViewController: UIViewController {
@IBOutlet weak var mealImageView: UIImageView!
@IBOutlet weak var mealName: UILabel!
@IBOutlet weak var mealPrice: UILabel!
@IBOutlet weak var explanation: UITextView!
var mealFromMeals = meals
/*
meals is Dictionary of meals. type is [String : [String : String]
example)
let meals = [
"FriedChicken" : [
"name" : "FriedChicken",
"image" : "fc.jpg",
"price" : "$13",
"exp" : "It is much delicious than KFC"
],
"Salmon Sushi" : [
"name" : "Salmon Sushi",
"image" : "s_sushi.jpg",
"price" : "&2",
"subImage" : "s_sushi_sub.jpg",
"exp" : "per 2pcs"
]
]
*/
var nameOfMeal = ""
var image = ""
var price = ""
var exp = ""
func showData() {
self.mealName?.text = nameOfMeal
if let meal = mealFromMeals[nameOfMeal] {
image = meal["image"]!
price = meal["price"]!
exp = meal["exp"]
}
self.mealImageView.image = UIImage(named: image)
self.mealPrice.text = "$" + price
self.explanation.text = exp
}
override func viewDidLoad() {
// Do any additional setup after loading the view.
showData()
super.viewDidLoad()
print(nameOfMeal)
}
}
When I select cell that has "sushi" as mealName, simulator prints "sushi" but view shows black screen.
(navigation worked well)
Let me know how to show the data of cell on the UIView.