【发布时间】: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