将核心数据从选定的表单元传递到新的视图控制器 [英] Pass core data from selected table cell to new view controller

查看:115
本文介绍了将核心数据从选定的表单元传递到新的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将选定表格单元格中的数据(标题,成分,步骤,图像)传递到新的视图控制器。但我不知道该怎么做。我有很多错误,所以现在我再次开始。谁能帮助我?我刚接触编码。感谢:-)我的代码:


$ b

  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! [配方]
} catch let err as NSError {
print(err.debugDescription)
}
}

func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {
如果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){
/ /我想将数据从表单单元传递到新的视图控制器(RECIPEDETAILVC)
}
}

func tableView(tableView:UITableView,canEditRowAtIndexPath indexPath:NSIndexPath) > Bool {
return true
}

func tableView(tableView:UITableView,commitEditingStyle editingStyle:UITableViewCellEditingStyle,forRowAtIndexPath indexPath:NSIndexPath){
if(editingStyle == UITableViewCellEditingStyle。删除){

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:[String:AnyObject]?)

imagePicker.dismissViewControllerAnimated(true,completion:nil)
recipeImage.image = image

}

@IBAction func addImage :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弱var recipeImage:UIImageView!
@IBOutlet weak var recipeTitle:UILabel!
@IBOutlet weak var recipeIngredients:UILabel!
@IBOutlet weak var recipeSteps:UILabel!

override func viewDidLoad(){
super.viewDidLoad()

//我想显示我选择的表格单元中的内核数据信息。
}
}

RECIPECELL.SWIFT / p>

  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()
}

}


解决方案

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



然后,在执行segue时使用。

  override func prepareForSegue(segue:UIStoryboardSegue,sender:AnyObject?){
if(segue.identifier ==RecipeDetail){
//我想通过数据从表单单元到新的视图控制器(RECIPEDETAILVC)
让recipeDetailControler = segue.destinationViewController as! RecipeDetailViewController

如果let mySelection = mySelection {
let recipe = recipes [mySelection]
//将此函数添加到
recipeDetailControler.configureRecipeData(recipe)
}
}
}

将此函数添加到RecipeDetailViewController:

  func configureRecipeData(recipe:Recipe){

//实现ME

}


I'm trying to pass data (title, ingredients, steps, image) from the selected table cell to a new view controller. But I don't know how to do that. I got lots of errors, so now I'm starting again. Can anyone help me? I'm new to coding. Thanks:-) My 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: [String : 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()
    }

}

解决方案

You need to track which item the person clicked.

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

Then, use that when doing the 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)
        }
    }
}

Add this function to RecipeDetailViewController:

func configureRecipeData(recipe: Recipe) {

    // IMPLEMENT ME

}

这篇关于将核心数据从选定的表单元传递到新的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆