Transmettre datatables de base de la cellule de tableau sélectionnée au nouveau controller de vue

J'essaie de transmettre des données (titre, ingrédients, étapes, image) de la cellule de tableau sélectionnée à un nouveau controller de vue. Mais je ne sais pas comment faire ça. J'ai eu beaucoup d'erreurs, alors maintenant je recommence. Quelqu'un peut-il m'aider? Je suis nouveau au encoding. Merci 🙂 Mon code:

VIEWCONTROLLER.SWIFT

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! var recipes = [Recipe]() override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self } override func viewDidAppear(animated: Bool) { fetchAndSetResults() tableView.reloadData() } func fetchAndSetResults(){ let app = UIApplication.sharedApplication().delegate as! AppDelegate let context = app.managedObjectContext let fetchRequest = NSFetchRequest(entityName: "Recipe") do { let results = try context.executeFetchRequest(fetchRequest) self.recipes = results as! [Recipe] } catch let err as NSError { print(err.debugDescription) } } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if let cell = tableView.dequeueReusableCellWithIdentifier("RecipeCell") as? RecipeCell { let recipe = recipes[indexPath.row] cell.configureCell(recipe) return cell } else { return RecipeCell() } } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return recipes.count } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { self.tableView.deselectRowAtIndexPath(indexPath, animated: true) } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if (segue.identifier == "RecipeDetail") { //I WANT TO PASS THE DATA FROM THE TABLE CELL TO THE NEW VIEW CONTROLLER (RECIPEDETAILVC) } } func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { return true } func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if (editingStyle == UITableViewCellEditingStyle.Delete) { let app = UIApplication.sharedApplication().delegate as! AppDelegate let context = app.managedObjectContext context.deleteObject(recipes[indexPath.row]) app.saveContext() recipes.removeAtIndex(indexPath.row) tableView.reloadData() } } } 

CREATERECIPE.SWIFT

 class CreateRecipeVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { @IBOutlet weak var recipeTitle: UITextField! @IBOutlet weak var recipeIngredients: UITextField! @IBOutlet weak var recipeSteps: UITextField! @IBOutlet weak var recipeImage: UIImageView! @IBOutlet weak var addRecipeBtn: UIButton! var imagePicker: UIImagePickerController! override func viewDidLoad() { super.viewDidLoad() imagePicker = UIImagePickerController() imagePicker.delegate = self recipeImage.layer.cornerRadius = 5.0 recipeImage.clipsToBounds = true } func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [Ssortingng : AnyObject]?) { imagePicker.dismissViewControllerAnimated(true, completion: nil) recipeImage.image = image } @IBAction func addImage(sender: AnyObject!) { presentViewController(imagePicker, animated: true, completion: nil) } @IBAction func createRecipe(sender: AnyObject!) { if let title = recipeTitle.text where title != "" { let app = UIApplication.sharedApplication().delegate as! AppDelegate let context = app.managedObjectContext let entity = NSEntityDescription.entityForName("Recipe", inManagedObjectContext: context)! let recipe = Recipe(entity: entity, insertIntoManagedObjectContext: context) recipe.title = title recipe.ingredients = recipeIngredients.text recipe.steps = recipeSteps.text recipe.setRecipeImage(recipeImage.image!) context.insertObject(recipe) do { try context.save() } catch { print("Could not save recipe") } self.navigationController?.popViewControllerAnimated(true) } } } 

RECIPEDETAILVC.SWIFT

 import UIKit import CoreData class RecipeDetailVC: UIViewController { @IBOutlet weak var recipeImage: UIImageView! @IBOutlet weak var recipeTitle: UILabel! @IBOutlet weak var recipeIngredients: UILabel! @IBOutlet weak var recipeSteps: UILabel! override func viewDidLoad() { super.viewDidLoad() //I WANT TO DISPLAY THE CORE DATA INFORMATION FROM THE TABLE CELL I SELECTED. } } 

RECIPECELL.SWIFT

 class RecipeCell: UITableViewCell { @IBOutlet weak var recipeTitle: UILabel! @IBOutlet weak var recipeImage: UIImageView! func configureCell(recipe: Recipe) { recipeTitle.text = recipe.title recipeImage.image = recipe.getRecipeImage() } } 

Vous devez suivre l'object sur lequel la personne a cliqué.

 var mySelection: Int? func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { self.tableView.deselectRowAtIndexPath(indexPath, animated: true) mySelection = indexPath.row } 

Ensuite, utilisez cela quand vous faites le segue.

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if (segue.identifier == "RecipeDetail") { //I WANT TO PASS THE DATA FROM THE TABLE CELL TO THE NEW VIEW CONTROLLER (RECIPEDETAILVC) let recipeDetailControler = segue.destinationViewController as! RecipeDetailViewController if let mySelection = mySelection { let recipe = recipes[mySelection] // add this function to your recipeDetailControler.configureRecipeData(recipe) } } } 

Ajoutez cette fonction à RecipeDetailViewController:

 func configureRecipeData(recipe: Recipe) { // IMPLEMENT ME }