我的词典中包含相同的键:
var food = [["item" : "Burger", "image": "burger"],
["item" : "Pizza", "image": "pizza"],
["item" : "Lunch", "image": "lunch"],
["item" : "Coffee", "image": "coffee"]],
它在我的表视图中使用。每个单元都显示出一个名称。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell
let foodObjects = food[indexPath.row]
cell.background.image = UIImage(named: foodObjects["image"]!)
cell.nameLabel.text = foodObjects["item"]
return cell
}
我想要的是,来自所选单元格的项目值会传递给第二个View Controller。 我知道我可以在整个函数中执行此操作,但我无法从所选项中获取值。有人能帮助我吗?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let indexPath = self.tableView.indexPathForSelectedRow?.row
let resVC = segue.destination as! RestaurantVC //second View controller
let selectedItem = food[indexPath.["item"]] // How to do this??
}
答案 0 :(得分:2)
创建像
这样的属性控制器上的var selectedItem。
On did选择将该项目保存到selectediItem。
比在准备segue方法中使用它。
答案 1 :(得分:1)
使用此方法从tableview
中选择特定对象 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//print(food[indexPath.row].first?.value)
}
答案 2 :(得分:1)
如果要从override func prepare(for segue: UIStoryboardSegue, sender: Any?)
函数中找到所选的索引路径。你可以使用tableView.indexPath(for cell: UITableViewCell)
。请尝试以下代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let destinationViewController = segue.destination as! RestaurantVC
if let selectedCell = sender as? UITableViewCell
{
let indexPath = tableView.indexPath(for: selectedCell)
let foodObjects = food[indexPath.row]
}
}
答案 3 :(得分:1)
像这样在RestaurantVC中创建一个属性
var selectedItem:String!
然后尝试这个
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let indexPath = self.tableView.indexPathForSelectedRow?.row
let resVC = segue.destination as! RestaurantVC //second View controller
resVC.selectedItem = food[tableview.indexPathForSelectedRow.row]["item"] // How to do this??
}
答案 4 :(得分:1)
请试试这个
var food: [Dictionary<String, String>] = [["item" : "Burger", "image": "burger"],
["item" : "Pizza", "image": "pizza"],
["item" : "Lunch", "image": "lunch"],
["item" : "Coffee", "image": "coffee"]]
您可以通过以下代码
在控制器selectedItem
中设置resVC
resVC.selectedItem = food[indexPath]["item"]!
或
let selectedItem = food[indexPath]["item"]
if let selectedItem = selectedItem {
resVC.selectedItem = selectedItem
}
答案 5 :(得分:1)
检查my answer here的可能性。使用委托从所选单元格(您已经传递到cellForRowAtIndexPath
上的单元格(如cell.nameLabel.text = foodObjects["item"]
)中检索数据,并在委托事件中执行segue以移动到新控制器并将您收到的数据作为参数到事件。所以它会像:
func foodItemCellSelected(using foodItem: [String]) // Delegate event of custom cell
{
self.selectedFoodItem = foodItem; // Retrieve selected item and save it in VC
self.performSegue(.....); // Perform segue
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let indexPath = self.tableView.indexPathForSelectedRow?.row
let resVC = segue.destination as! RestaurantVC //second View controller
resVC.foodItem = self.selectedFoodItem
}