Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - textField in a tableViewCell - Scrolling off screen clears the value

I have a textField inside a tableViewCell. I recently noticed that if I enter a value in the textBox, and then scroll the cell off the screen, the value in the textbox disappears.

I have a safeguard against this. If there is no UIKeyboard present, the tableView can't scroll far enough to have to redraw the cell. However, some users don't use the done key on the keyboard to make the keyboard go away, and instead scroll the table up, breaking the program.

Is there a way to immediately store the textField string to a variable, or keep the tableView from scrolling, or make the keyboard disappear if the user tries to scroll the tableView?

I don't have a NIB file for this view, it's a programmatic tableViewController, so I can't do anything in Interface Builder with invisible custom buttons, as suggested in other questions on StackOverflow.

like image 993
Daddy Avatar asked Feb 22 '10 21:02

Daddy


1 Answers

Are you instantiating the text field inside your cellForRowAtIndexPath:: method? If so, it may be re-instantiating each time the cell scrolls into view.

You should save a reference to the text field on your controller and reuse it in your cellForRowAtIndexPath::

like image 88
chrissr Avatar answered Oct 05 '22 13:10

chrissr