【问题标题】:Pass array from tableview to tableview [duplicate]将数组从 tableview 传递到 tableview [重复]
【发布时间】:2018-05-14 07:48:12
【问题描述】:

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

问题:在preparingForSegue中无法从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(显示员工姓名) 代码在这里一切正常,我在这里唯一苦苦挣扎的是将信息传递给下一个视图控制器。在评论部分,我尝试输入 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 上的数组。

【问题讨论】:

  • 当你说“这没有按预期工作”时,实际发生了什么
  • Daniel,链接显示字符串。我正在寻找数组。请正确阅读问题。
  • @SwiftQuestions,你打算通过 segue 在视图控制器之间传递什么 type 数据并不重要;如果您创建一个完全自定义的数据类型,则无需重新学习如何传递它的概念。

标签: ios arrays swift uitableview


【解决方案1】:

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

这样做:

  • 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
}

【讨论】:

  • 嘿 Vadian,我试过 let employee = lists[indexPath.row] 并得到错误“无法使用类型为 '[indexPath]' 的索引的 [Employee] 类型的值的下标
  • 我建议的代码使用indexPathForSelectedRow,你的代码是indexPathsForSelectedRows,请注意区别
  • 对不起!我刚注意到!让我再试一次。
【解决方案2】:

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

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

【讨论】:

  • 我的程序在运行后立即崩溃。
【解决方案3】:

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

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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多