ios - 如何将数据从表视图行操作的标签发送到视图控制器?
问题描述
我正在尝试通过单元格的“编辑”表格视图行操作将数据从表格视图单元格传递到另一个视图控制器。
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let edit = UITableViewRowAction(style: .normal, title: "Edit") { (action, indexPath) in
let editViewController = self.storyboard?.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
self.present(editViewController, animated: true, completion: nil)
let cell = self.recipeList[indexPath.row] as? RecipeTableViewCell
editViewController.nameTextField.text = cell?.recipeNameLabel.text
}
edit.backgroundColor = UIColor(red: 167.0/255.0, green: 204.0/255.0, blue: 181.0/255.0, alpha: 1.0)
return [edit]
}
这是我用于行行为功能的单元格...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RecipeCell", for: indexPath) as! RecipeTableViewCell
let recipe = recipeList[indexPath.row]
cell.recipeNameLabel!.text = recipe.name
cell.ratingControl.rating = recipe.rating
}
return cell
}
解决方案
不要传递目标 VC 中的每个值,而是尝试recipe
仅传递对象。然后从该对象获取值并填写您的控件。
1)在你的创建recipe
对象EditViewController
var editRecipe : Recipe! // Your Model class
2) 赋值
let editViewController = self.storyboard?.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
editViewController. editRecipe = recipeList[indexPath.row]
self.present(editViewController, animated: true, completion: nil)
3)ViewDidload
在EditViewController
if editRecipe != nil {
//Fill data in lbls
//lbl_name.text = editRecipe.name
}
推荐阅读
- angular - 自定义标题 Ag-Grid 中的排序数字?
- javascript - JavaScript RegEx 用于匹配长度在 1 到 5 个字符之间且不应以空格开头和结尾的字符串
- angular - 错误:角度 4 或角度 6 中的 StaticInjectorError(AppModule)
- javascript - 安装屏幕阅读器
- php - 在 php 中使用 ssh 连接到远程服务器
- javascript - 如何在节点js中的foreach之后发送respose?
- html - 使用 XML 显示图像列表
- laravel - 在 lumen 中,如何在 trait 中调用另一个 trait 的函数
- angular - 将用户对象存储在 localstroage 中是否合适?
- asp.net - Telerik Kendo MVC 调度程序 - 无法移动事件