# Textfield not entering data into textfield

**URL:** <https://forums.kodeco.com/t/textfield-not-entering-data-into-textfield/39792>\
**Category:** Swift\
**Created:** [February 25, 2018, 6:12pm UTC](https://forums.kodeco.com/t/textfield-not-entering-data-into-textfield/39792 "2018-02-25T18:12:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [February 25, 2018, 6:12pm UTC](https://forums.kodeco.com/t/textfield-not-entering-data-into-textfield/39792/1 "2018-02-25T18:12:51Z")

</div>

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

    }

```

 ![20 PM](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/0/a/0ae5d88ce4b2783e85dff88c6d0d5261bd24a070.png)

---

<div class="post-metadata">

**Author:** ![joadan](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/joadan/32/102075_2.png) [@joadan](https://forums.kodeco.com/u/joadan)\
**Post date:** [February 25, 2018, 7:22pm UTC](https://forums.kodeco.com/t/textfield-not-entering-data-into-textfield/39792/2 "2018-02-25T19:22:16Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [August 11, 2018, 10:18am UTC](https://forums.kodeco.com/t/textfield-not-entering-data-into-textfield/39792/3 "2018-08-11T10:18:39Z")

</div>

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