YichenBman
YichenBman

Reputation: 5661

Save textField.text from Custom UitableViewCell

Okay so I have an app that stores users info pretty similar to the way that the contacts app works. I'm using a few tableViews with custom UITableViewCells that each have UITextFields in them.

I have a save button that triggers a segue that adds all of the textField.text entered into an array of dictionaries

newContact =

            [   "FirstName" : "\(cell.firstNameTextField.text)",
                "LastName" : "\(cell.lastNameTextField.text)",
               .........

            ]

I'm not sure how to move forward. I know its not going to get the right text entered without someway to decide the cells indexPath.

Anyone see a way through this?

Again to reiterate, I've got two tableViews, with a few different custom UITableViewCells. Each cell has a textField. I want the user to be able to type text in, then have that textField.text stored

Upvotes: 0

Views: 781

Answers (1)

Alex Koshy
Alex Koshy

Reputation: 1661

I would recommend pairing an NSObject model class with your UITableView to locally store the information until needed. Depending on the amount of rows in the tableView, this data may need to be referenced again, should the user need to reuse a cell. This can come in handy in cellForRowAtIndexPath.

Implementing the UITextFieldDelegate protocol in your controller will allow you to:

  • Respond to textFieldDidEndEditing and save textField.text to your model object (located in an array or collection type of your preference).

NOTE: Make sure to set the delegates for your textFields to self for this to work.

Then, when you want to save, you can iterate through the array of model objects and pull what you need from there to create your array of dictionaries.

Upvotes: 3

Related Questions