ToDo List with CoreData

//  ViewController.swift
//  PracticeCoredata


import UIKit
import CoreData

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBAction func btnNavBar(_ sender: UIBarButtonItem) {
        let AVC : AdddViewController = self.storyboard?.instantiateViewController(withIdentifier: "AdddViewController") as! AdddViewController
        
        self.navigationController?.pushViewController(AVC, animated: true)
      }

    @IBOutlet var tblView: UITableView!
    
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
    var User1 = [User]()
    
    override func viewWillAppear(_ animated: Bool) {
        do{
            User1 = try context.fetch(User.fetchRequest())
        }
        catch
        {
        print("Failed Fetching")
        }
        tblView.reloadData()
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
            tblView.isUserInteractionEnabled = true
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return User1.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
        
        cell.lblTitle.text = User1[indexPath.row].title
        cell.lblDescription.text = User1[indexPath.row].desc
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        let dataRow = self.User1[indexPath.row]
        
        if editingStyle == .delete{
            self.User1.remove(at: indexPath.row)
            self.context.delete(dataRow)
            
            do
            {
                try self.context.save()
            }
            catch
            {
                print(error.localizedDescription)
            }
            self.tblView.reloadData()
        }
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let alert = UIAlertController(title: "Edit", message:"Edit in your List" , preferredStyle: .alert)
        
        let done = UIAlertAction(title: "Done", style:.default)
        {
            (action) in
            
            
        let titletxt = alert.textFields![0] as UITextField
        let descriptiontxt = alert.textFields![1] as UITextField

            
        self.User1[indexPath.row].title = titletxt.text
        self.User1[indexPath.row].desc = descriptiontxt.text
            
        (UIApplication.shared.delegate as! AppDelegate).saveContext()
        self.tblView.reloadData()
            
        }
        
        let Cansle = UIAlertAction(title: "Cansel", style: .cancel)
        {
            (action) in
            self.resignFirstResponder()
        }
        

        alert.addTextField { (TextField : UITextField!) -> Void in
            
            TextField.text = self.User1[indexPath.row].title
            TextField.backgroundColor = UIColor.lightGray
            
        }
        
        alert.addTextField { (TextField : UITextField!) -> Void in
            
            TextField.text = self.User1[indexPath.row].desc
            TextField.backgroundColor = UIColor.lightGray
        }
      alert.addAction(done)
      alert.addAction(Cansle)
        
      self.present(alert, animated: true, completion: nil)
        
        
    }

}




//  AdddViewController.swift
//  PracticeCoredata



import UIKit
import CoreData

class AdddViewController: UIViewController {
    
    @IBOutlet var txtTitle: UITextField!

    @IBOutlet var txtDescription: UITextField!
    
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    
    @IBAction func btnAdd(_ sender: UIButton) {
        
        let User1 = User(context: context)
        
        User1.title = txtTitle.text
        User1.desc = txtDescription.text
        
        (UIApplication.shared.delegate as! AppDelegate).saveContext()
        
        self.navigationController?.popViewController(animated: true)
    }
       override func viewDidLoad() {
        super.viewDidLoad()

        
    }

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


Comments