Я настроил метод подготовки к переходу и считаю, что успешно отправил свои данные во второй ViewController, но не знаю, как использовать переданные данные.
Например:
Когда пользователь нажимает Белок, который я хочу отправить, выбран второй белок tableViewController, а затем заполняет его массивом белков.
Ниже мой первый код таблицы:
Первое представление таблицы:
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)
}
}
}
}
В настоящее время, если я печатаю отправляемое значение, оно работает. В этом случае он печатает «Белок». Но в идеале это то, что я хочу, но я не уверен, как это сделать.
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
}
Все сообщения, которые я нашел, касаются отправки данных TableView в обычный viewController, который я не нашел полезным при попытке его реализовать. Я новичок в Swift, поэтому любые советы приветствуются.