Steaphann
Steaphann

Reputation: 2777

save data inside a UITableView

I have a custom UITableView with all kinds of cells. I build this tableview by creating for each section a subClass of UITableViewCell. You can see my form over here. Like you can see is, I have textfields, textViews and switches. At the bottom of my form (not on the screenshot) I have a save button.

I made a Person class with the following properties.

  1. login
  2. Password
  3. Methods (this is an array)
  4. phone
  5. remarks

In my viewDidLoad I load up the tableView with data from my persons object. But now I want when a users changes some fields and it pushed on the save button, all the fields are saved into the persons object.

Problems I have

Anybody got an idea how I can do this ? Kind regards

enter image description here

Upvotes: 1

Views: 729

Answers (2)

Anton
Anton

Reputation: 4018

UITableViewCell uses a caching mechanism where it's contents are reloaded when it is scrolled off the screen and back on. What this means is, by scrolling the cell out of the screen bounds and then scrolling it back onto the screen, you are redrawing the cell.

The solution is to use a data source in your table view controller:

  • Use the data source to load the value that should be shown in the cell (or a subview of the cell such as a UITextField) in your tableView:cellForRowAtIndexPath: method.
  • Save the contents of the cell to the data source when a cell's contents (or the contents of a subview such as a UITextField) us updated.

Hope that helps!

Upvotes: 2

Vishal Singh
Vishal Singh

Reputation: 4490

its because you are not updating the array from which you are populating your cell, now when you scroll your tableView, and then scroll up again, your cells are created again and theses cells get there textfield's text from the array which still has old values. try updating your array from which you populate your tableview in the textfield's didEndEditing delegate method.

Upvotes: 0

Related Questions