将数组从tableview传递到tableview

时间:2017-11-30 11:33:39

标签: ios arrays swift uitableview

目标:使用创建的数据将一组信息从tableview传递到另一个tableview。

问题:在准备ForSegue中无法从TableViewController访问数组信息

结果: TableViewController包含员工姓名,当点击每个员工时,它会显示显示信息数组的详细信息。

1)Employee.swift(数据模型)

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(显示员工的姓名) 这里的一切都很好用代码,我在这里努力的唯一事情是将信息传递给下一个viewcontroller。在评论部分,我尝试输入 destination.food = lists [indexPath.row] .food 。这没有按预期工作。我正在尝试检索数组信息。

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(显示一组信息) 代码工作正常。我有创建一个空数组的想法,信息将从TableViewController传递。在评论部分,我将写 cell.textLabel?.text = food [indexPath.row] 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
}

请指教。我在过去的几个小时里一直在研究,但还无法解决这个问题。请记住,所有代码在这里工作正常,我只是在访问阵列信息时苦苦挣扎。

编辑:我正在寻找要访问的数组并在DetailsTableViewController上显示。

3 个答案:

答案 0 :(得分:2)

根据您的问题,您将把一个项目(员工)传递给详细视图控制器,而不是数组。

那样做:

  • VillageTableViewController中使用

    替换prepare(for
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showVillages",
           let indexPath = self.tableView.indexPathForSelectedRow {
           let employee = lists[indexPath.row]
           let destination = segue.destination as! DetailsViewController
           destination.employee = employee
        }
    }
    
  • DetailsViewController中创建一个属性employee

    var employee : Employee!
    

现在,您可以访问详细信息视图控制器中employee的所有属性,例如使用其food数组作为数据源

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return employee.food.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! DetailsTableViewCell
   cell.textLabel.text = employee.food[indexPath.row]
   return cell
}

PS:我建议使用第二个结构(在这种情况下,你不需要在两个结构中都写任何初始化器)而不是单独的食物(名称)和成分数组。

struct Food {
    let name : String
    let ingredients: [String]
}


struct Employee {
    let name: String
    let food: [Food]
}

var lists : [Employee] = [
    Employee(name: "Adam" , food: [Food(name: "Fried Rice", ingredients:["Insert oil, cook"]),
                                   Food(name: "Fried Noodles", ingredients: ["Insert oil, cook"])])

好处是您可以轻松使用详细视图控制器中的部分。

func numberOfSections(in tableView: UITableView) -> Int {
    return return employee.food.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let food = employee.food[section]
    return return food.name
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let food = employee.food[section]
    return food.ingredients.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! DetailsTableViewCell
   let food = employee.food[indexPath.section]
   cell.textLabel.text = food.ingredients[indexPath.row]
   return cell
}

答案 1 :(得分:0)

仔细看看self.tableView.indexPathsForSelectedRows是一个IndexPaths数组。你为什么不试试

destination.food = lists[indexPath[0].row].food

答案 2 :(得分:0)

根据列表中选定的员工准备食物

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showVillages" {
        guard let indexPath = tableView.indexPathsForSelectedRow,
            let destinationVC = segue.destination as? DetailsViewController else {
            return
        }
        destinationVC.food = lists[indexPath.row].food
    }
}
相关问题