views:

567

answers:

3

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.

+3  A: 

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::

chrissr
I use a case statement block to add the textField to Section 0, Row 2. The textField is declared in the header as a pointer, so I can access the properties by using myTextField.text.Does this help?
JustinXXVII
I'm not quite following. Are you saying in your cellForRowAtIndexPath method you have a case statement that determines if you're at Section 0, Row 2 and if so, it adds the text field?If so, where are you calling [[UITextField alloc] init]? If it's also inside the cellForRowAtIndexPath method, then you're probably re-initializing the text field each time the row scrolls into view (and cellForRowAtIndexPath is called).Try initializing your textfield in loadView or your controller's main init method.
chrissr
A: 

The answer to this question is to implement the ScrollView delegate method willBeginDragging. UITableView inherits from UIScrollView.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
       [textField resignFirstResponder];
}

This dismisses the keyboard as soon as the tableView begins scrolling

By this inheritance, it is also possible to keep the tableView from scrolling, period. Set the scrollEnabled property to NO, a la

self.tableView.scrollEnabled = NO;
JustinXXVII
A: 

You should be setting the delegate of the NSTextField you created to the controller and then implement textField:shouldChangeCharactersInRange:replacementString: in your controller so that you are notified anytime the text changes in the text field.

When the text changes store the changed text in a property of your controller.

When you create the NSTextField in cellForRowAtIndexPath set the value from that property.

Remember that with Cocoa you need to keep your model data separate from your view.

Gerry
But what happens when the textField is scrolled up, and the value is deleted. Does the instance variable lose its value as well?
JustinXXVII
No, when delegate calls textField:shouldChangeCharactersInRange:replacementString you should copy the string from the text field into an instance property so that it will be valid after the UITableViewCell is deleted.
Gerry