开发者

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 开发者_StackOverflow中文版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.


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


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;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜