A
A
Aleksandr Govorukhin2017-06-11 22:22:53
Swift
Aleksandr Govorukhin, 2017-06-11 22:22:53

How to update Core Data from swift 3 table cells?

Good night!)
The task for the experienced is very simple, for me it is not entirely clear. There are 3 static cells with UITextField, how to make it so that by clicking on the "save" button - overwrite the data in the database?

//
//  EditRecipeTableViewController.swift
//  Customer
//
//  Created by Aleksandr Govorukhin on 11.06.17.
//  Copyright © 2017 Aleksandr Govorukhin. All rights reserved.
//

import UIKit
import MagicalRecord

class EditRecipeTableViewController: UITableViewController {
    
    @IBOutlet weak var imageCollectionRecipe: UICollectionView!
    
    var recipe: Recipe!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 12
        tableView.separatorStyle = .none
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! EditRecipeCellTableViewCell
        
        //Отображение информации в ячейки
        
        switch indexPath.row {
        case 0:
            cell.textEdit.text = self.recipe.nameRecipe
            
        case 1:
            cell.textEdit.text = self.recipe.descriptionRecipe
            
        case 2:
            cell.textEdit.text = self.recipe.sourceLink
            cell.textEdit.font = UIFont.systemFont(ofSize: 12.0)
            
        default:
            print("Critical ERROR in Switch as EditRecipeTableViewController")
        }
        
        return cell

    }

    
    @IBAction func saveEditRecipe() {
      
        
        
    }
    
    
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aleksandr Govorukhin, 2017-07-07
@SnapSh0t

@IBAction func saveEditRecipe() {
        
        let CDataArray = newImages as! NSMutableArray
        
        if let recipe = recipe {
            
            if recipe.value(forKey: "nameRecipe") != nil {
                recipe.setValue(textEditName.text, forKey: "nameRecipe")
            }
            
            if recipe.value(forKey: "descriptionRecipe") != nil {
                recipe.setValue(textEditDescription.text, forKey: "descriptionRecipe")
            }
            
            if recipe.value(forKey: "imageRecipe") != nil {
                recipe.setValue(NSKeyedArchiver.archivedData(withRootObject: CDataArray), forKey: "imageRecipe")
            }
            
        } else {
            print("unable to fetch or create recipe")
        }
        
        
        (UIApplication.shared.delegate as! AppDelegate).saveContext()
        
        self.goToListOfRecepies()
    }

G
GOODDUDE, 2017-07-06
@GOODDUDE

For pulling data follow the example

func getData(){
        var example: [Entity Name] = []
        do
        {
            example = try context.fetch(Example.fetchRequest())
            for ex in example {   
                let examp = ex as! Entity name
                print(examp.value)
            }
        }
        catch
        {
            print(error)
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question