SnapSh0t
@SnapSh0t
iOS-Developer

Как обновить данные Core Data из ячеек таблицы swift 3?

Доброй ночи!)

Задача для опытных очень простая, для меня не совсем понятная. Есть 3 статические ячейки с UITextField, как сделать так, чтобы по нажатию на кнопку "сохранить" - перезаписать данные в бд?
//
//  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() {
      
        
        
    }
    
    
}
  • Вопрос задан
  • 654 просмотра
Решения вопроса 1
SnapSh0t
@SnapSh0t Автор вопроса
iOS-Developer
@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()
    }
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
GOODDUDE
@GOODDUDE
Swift Developer
Для вытягивания данных следуйте примеру
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)
        }
    }
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы