Objective: Pass an array of information from tableview to another tableview using the data created.
Problem: Couldn't access the array information from TableViewController in preparingForSegue
Result: TableViewController contains employee names, and when click on each of the employee, it goes to the details of showing arrays of information.
1) Employee.swift (model for data)
struct Employee {
var name: String
var food: [String]
var ingredients: [String]
init(name: String, food: [String], ingredients: [String]) {
self.name = name
self.food = food
self.ingredients = ingredients
}
}
2) TableViewController.swift (shows the name of the employee) Everything is working well here with the codes, the only thing that I am struggling here is passing the information to the next viewcontroller. In the comment section is where I tried typing destination.food = lists[indexPath.row].food. This didn't work as expected. I am trying to retrieve the array information.
class VillageTableViewController: UITableViewController {
var lists : [Employee] = [
Employee(name: "Adam" , food: ["Fried Rice","Fried Noodles"], ingredients: ["Insert oil, cook","Insert oil, cook"])]
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showVillages" {
if let indexPath = self.tableView.indexPathsForSelectedRows {
let destination = segue.destination as? DetailsViewController
// pass array of information to the next controller
}
}
}
3) DetailsTableViewController.swift (shows an array of information) Code is working fine. I have the idea of creating an empty array and the information will be passed from TableViewController. In the comment section, I will then write cell.textLabel?.text = food[indexPath.row] and cell.subtitle?.text = ingredients[indexPath.row]
class DetailsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var food:[String] = []
var ingredients:[String] = []
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! DetailsTableViewCell
//this is where I will get the information passed from the array
return cell
}
Please advise. I've been researching for the past few hours and couldn't figure it out yet. Bear in mind that all the code works fine here and I've only struggling in accessing the array information.
Edit: I am looking for arrays to be accessed and to be displayed on DetailsTableViewController.