【问题标题】:How would I send data from labels from a table view row action to a view controller?如何将标签中的数据从表视图行操作发送到视图控制器?
【发布时间】:2019-01-16 11:56:15
【问题描述】:

我正在尝试通过单元格的“编辑”表格视图行操作将数据从表格视图单元格传递到另一个视图控制器。

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
}

【问题讨论】:

  • 你应该重复使用你的细胞!不要为您拥有的每一行存储一个单元格!
  • 也显示您的cellRowAt 方法

标签: ios swift uitableview uitableviewrowaction


【解决方案1】:

不要在目标 VC 中传递每个值,而是尝试仅传递 recipe 对象。然后从该对象中获取值并填写您的控件。

1) 在您的 EditViewController 中创建 recipe 对象

 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
}

【讨论】:

    猜你喜欢
    • 2014-03-07
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多