I have setup a prepare for segue method and I believe I have successfully sent my data to the second ViewController but I'm unsure how to use the passed data.
For Example:
When the user taps Protein I want to send to the second tableViewController protein was selected and then populate it with an array of protein.
Below is my first tableview code:
First TableView:
class OrdersTableViewController: UITableViewController {
var titleList = ["Protein","Protein Flavor", "Base", "Base Flavor", "Side", "Additional"]
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titleList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! OrdersTableViewCell
cell.cellTitle.text = titleList[indexPath.row]
// Configure the cell...
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showDetailView") {
let DVC = segue.destination as! OrderDetailTableViewController
if let indexpath = self.tableView.indexPathForSelectedRow {
let proteins = titleList[indexpath.row] as String
DVC.sentData1 = proteins
print (proteins)
}
}
}
}
Currently if I print the value that's being sent it seems to be working. In this case it prints "Protein". But ideally this is what I want, but I'm unsure how to do it.
class OrderDetailTableViewController: UITableViewController {
var sentData1:String!
var proteinList = ["Salmon", "Meatballs", "Chicken", "Cod","Sausage", "Frittata"]
var baseList = ["White Rice", "Brown Rice"]
//take what is selected from sentData1 and populate second tableview
if sentData1 == "Protein" {
//populate tableview with proteinList
}
if sentData1 == "Base" {
//populate tableview with baseList
}
All the posts I've found deal with a TableView sending data to a normal viewController which I haven't found useful when trying to implement it. I'm brand new to Swift so any tips are appreciated.