繁体   English   中英

如何将标签中的数据从表视图行操作发送到视图控制器?

[英]How would I send data from labels from a table view row action to a view controller?

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

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)在您的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)在ViewDidloadEditViewController

if editRecipe != nil {
    //Fill data in lbls
    //lbl_name.text = editRecipe.name
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM