thephatp
thephatp

Reputation: 1489

Saving Input in UITableView w/ custom cells?

I'm having a difficult time finding how to do true data binding for UITableViews. I currently have a table view that has a number of custom cells, most of which are simple, and one of which is not. Also, I'm reusing cells as necessary, so at some point, data from any given cell can scroll off the screen, at which point the data is lost from a UI perspective. I need to know the best way (best practice) to get this information and save it.

I can usually show about 4-5 cells at any given time.

Currently all of my data is input via text fields (though this could change soon, as I may add a toggle switch to one of the cells). Each time the user touches out of a text field, textFieldDidEndEditing is called. I then save the data based off of some information from the text field (like tag or placeholder text). I do this because I need to get the data and save it before that cell scrolls out of view, at which point I could no longer save the data by getting the text from the UITextField.

I can't help but believe there is a better way to do this. My first thought was to find a way to know that a given cell is about to scroll out of view (and thus likely lose it's value b/c it will be reused), and just save the data at that point. I can't find anything in the UITableViewController, UITableView, or UITableViewCell class references that hint at anything like this. So, I'm guessing that is not the way to go.

The other thought I had was data binding, which seems the most appropriate, but I can't find any good examples of this. I need to have a way to bind data to custom objects.

For example, I have an object, say, Contact. This object has a string field for name, a string field for notes, and an Address field (another object) that has it's own set of fields (like, street1, street2, city, state, zip). I would like to bind these values to the table view such that when a field has been edited, it is saved to my internal object.

Q1: Is there a way to do this?

FYI, the string fields for Contact will share custom cell types, but the Address will get it's own custom cell with all of the text fields in it. I'm not making different cells for each field in the address, but rather one custom cell with 4 or 5 text fields in it.

To complicate things a little further, I may need to swap out the custom address cell to match that of a different country. So now I need to bind the Address object (ref) to the new custom address cell.

Q2: Does this change the answer to Q1 above?

Again, I'm looking for a better way to save values from custom UITableViewCells with UITextFields, and I have to consider the fact that the data can scroll off the screen and a cell be reused, so not all of the data I need is available to me by the time the user exits the UITableView.

Any help or guidance is greatly appreciated. Even just pointing me to a good resource that I have yet to find.

Upvotes: 1

Views: 1289

Answers (2)

jlehr
jlehr

Reputation: 15617

Also, I'm reusing cells as necessary, so at some point, data from any given cell can scroll off the screen, at which point the data is lost from a UI perspective.

Typically, in an editable detail view there's no need to reuse UITableViewCell instances. If, instead of creating cells dynamically in tableView:cellForRowAtIndexPath: you create the cells in Interface Builder or in your init... or viewDidLoad methods, you can cache them in instance variables so you won't have to worry about user data disappearing as a side-effect of scrolling.

I would like to bind these values to the table view such that when a field has been edited, it is saved to my internal object

You should be able to use NSKeyValueObserving methods to directly observe changes to the text properties of UITextField instances. You could take advantage of that to propagate changes, though you'd need to implement some custom functionality in your controller (or elsewhere) to bind the text fields to key paths.

Upvotes: 0

Ole Begemann
Ole Begemann

Reputation: 135578

Currently all of my data is input via text fields (though this could change soon, as I may add a toggle switch to one of the cells). Each time the user touches out of a text field, textFieldDidEndEditing is called. I then save the data based off of some information from the text field (like tag or placeholder text). I do this because I need to get the data and save it before that cell scrolls out of view, at which point I could no longer save the data by getting the text from the UITextField.

I can't help but believe there is a better way to do this.

In my opinion, this is exactly the way to do it. The views should never hold the data. The consequence is that, as soon as the user edits a view, the new data should be saved in the model.

The other thought I had was data binding, which seems the most appropriate, but I can't find any good examples of this. I need to have a way to bind data to custom objects.

Q1: Is there a way to do this?

No, not on the iPhone. Cocoa on OS X has something like this in the form of Cocoa Bindings in combination with NSArrayController, but this feature is not available on iOS. You have to go the manual route.

Upvotes: 1

Related Questions