[英]How to get a selected value from dictionary through tableView?
我有一本具有相同键的字典:
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??
}
创建像
控制器上的var selectedItem。
选中确实将项目保存到selectediItem。
比在准备序列方法中使用它。
使用此方法从tableview中选择特定对象
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//print(food[indexPath.row].first?.value)
}
如果要从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]
}
}
像这样在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??
}
请尝试这个
var food: [Dictionary<String, String>] = [["item" : "Burger", "image": "burger"],
["item" : "Pizza", "image": "pizza"],
["item" : "Lunch", "image": "lunch"],
["item" : "Coffee", "image": "coffee"]]
您可以通过以下代码在控制器resVC
设置selectedItem
resVC.selectedItem = food[indexPath]["item"]!
要么
let selectedItem = food[indexPath]["item"]
if let selectedItem = selectedItem {
resVC.selectedItem = selectedItem
}
可以在这里查看我的答案 。 使用委托从选定的单元格中检索数据(已将其传递到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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.