I have a paper based form with a number of different sections, with each section having a number of different questions. I am displaying this as a UITableView. Each question is displayed as a UITableViewCell with the question itself, and a UITextField for an answer. The UITableView is currently backed by two object types, Section and Question – with each Section object containing an array of Questions.
I’m wondering how I can easily connect any editing that occurs in the UITableView with the objects. For example, if the user types in an answer to question 1, I’d like to then be able to assign that answer text to the appropriate Question object.
What I’m currently doing is giving each UITextField a unique tag, and then storing a pointer to each Question object in a dictionary with the tag values as the keys. Then when the appropriate UITextField delegate method fires, I look up the object in the dictionary through the UITextField tag.
This seems ugly to me, and I’m not too sure what happens if the UITableViewCell goes offscreen and is re-used elsewhere in the table. Is there a better way that I’m missing?
Unless I’m misunderstanding the question, why not just use the UITextFieldDelegate protocol to tell the object that the text field has finished editing.
You’d simply do the following:
Make your Question a UITextFieldDelegate. Then define textFieldDidEndEditing in your Question implementation class
That should do it right? When the text field has finished, the Question object gets the answer.