Textfield not entering data into textfield

My code uses a textfield to enter data into core data. Then have the core data displayed on a tableview. The problem is nothing is being displayed on the tableview. Don’t know why?

                import UIKit; import CoreData

class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet var fewFeetBack: UITableView!
    var itemName: [NSManagedObject] = []
    var titleTextField: UITextField!


    @IBAction func addBUTTON(_ sender: Any) {
        print("d")

        let alert = UIAlertController(title: "add", message: "now", preferredStyle: .alert)
        let addAction = UIAlertAction(title: "ADD", style: .default, handler: self.dd)
        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)

        alert.addAction(addAction)
          alert.addAction(cancelAction )
          alert.addTextField(configurationHandler: titleTextField)

        self.present(alert, animated:  true, completion:  nil)

    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let appDelage = UIApplication.shared.delegate as! AppDelegate
        let context = appDelage.persistentContainer.viewContext
        let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Title")

        do {
            itemName = try context.fetch(fetchRequest)

        }
        catch{
            print("judo")
        }
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCellEditingStyle.delete{
            let appDDElageate = UIApplication.shared.delegate as! AppDelegate
            let context = appDDElageate.persistentContainer.viewContext
           context.delete(itemName[indexPath.row])
            itemName.remove(at: indexPath.row)

        }
    }
    func titleTextField(textfield: UITextField!){
        titleTextField = textfield
        titleTextField.placeholder = "ItemName"
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemName.count

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let title = itemName[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = title.value(forKey: "Title") as? String
        return cell

    }
    func dd(alert: UIAlertAction!)
    {
        let appDeleage = UIApplication.shared.delegate as! AppDelegate
        let context = appDeleage.persistentContainer.viewContext
        let entity = NSEntityDescription.entity(forEntityName: "Title", in: context)

        let theTitle = NSManagedObject(entity: entity!, insertInto: context)
        theTitle.setValue(titleTextField.text, forKey: "Title")

        do {
            try context.save()
            itemName.append(theTitle)
        }
        catch {
            print("judo")
        }
    }

    }

First of all you have

title.value(forKey: "Title") as? String

and I think the key is case sensitive so you should use “title” instead to match the attribute.

Secondly, since you have a Title entity why not use that in your array, like

var itemName: [Title] = []

that way you could do

cell.textLabel?.text = title.title

This topic was automatically closed after 166 days. New replies are no longer allowed.