1

I have a UITableView with static custom UITableViewCells, I need to scroll a text field into view as currently they are getting hidden behind the keyboard after the return key is pressed and the next responder is set. I know i need to use scrollToRowAtIndexPath:atScrollPosition:animated:. How would I go about scrolling to one of the text boxes?

I know this should scroll itself but if your UIViewController derives from UITableViewController (as Apple states it should be) then the UITableViewController class handles this behaviour for you by implementing UITextFieldDelegate, UIScrollViewDelegate etc. My application stopped doing this as I changed to derive from UIViewController and add the table view on top of the view controller's UIView. So basically I'm missing the features from UITableViewController because I (for other reasons) choose to derive from UIViewControler.

jszumski
  • 7,430
  • 11
  • 40
  • 53
MrBeanzy
  • 2,286
  • 3
  • 28
  • 38

3 Answers3

1

I do this all the time. The way that I do it is I have a method that is called in the UIControlEventEditingDidBegin for the textfield, and in that method, I do:

-(void)startEdit:(UITextField *)textField {
    self.prevOffset = self.tableView.contentOffset.y; //I like storing the current offset so I can restore it when the text stops editing, you don't have to do this.
    int offSet = [textField superview].frame.origin.y; //this gets the y coordinate of the cell the textField is in. If the table is not at 0,0, you also need to add [[textField superview] superview].frame.origin.y;
    offSet-=(self.view.frame.size.height-KEYBOARD_HEIGHT)/2; //where KEYBOARD_HEIGHT is 216 in portrait and 160 in landscape;
    if (offSet<0) offSet = 0;
    [UIView animateWithDuration:0.3 animations:^{
        [self.tableView setContentOffset:CGPointMake(0,offSet)];}];
}

I do a lot of other things as well, but I believe they are specific for my application.

First, if the offset is greater than 0, I set teh contentInset to UIEdgeInsetsMake(0,0,KEYBOARD_HEIGHT,0) because I was having some jumpy scrollViews before I did that.

Also, if the original offset (self.prevOffset) plus the frame's height is greater than the content size (which would also cause jumping as it sets the offset too low then jumps back), I set the prevOffset to MAX(0,contentSize.height-frame.size.height).

These things aren't neccessary, but you are getting Scroll/TableViews that are jumping around, try them out.

Jsdodgers
  • 5,253
  • 2
  • 20
  • 36
0

UITableViewController has the delegate protocols already in the header field. Since your class is no longer a UITableViewController, you need to manually add the delegate protocol headers for a UITableView to your .h file.

When I create a custom view controller that has a UITableView, I start off with a UITableViewController too, just to get the delegate methods, but then I change UITableViewController to UIViewController as you have done, and manually add the Delegate protocols to the header.

If you want, you can look at UITableViewController.h and copy over the delegate protocols.

For your reference they are:

<UITableViewDelegate, UITableViewDataSource>

So your .h file should look similar to this:

@interface MyTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

Also, don't forget to set the delegates of the controller to the file's owner in Interface builder, or to self in code.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
0

You may also find it a lot easier to use a framework such as the freely available Sensible TableView framework. These frameworks usually provide all the data entry cells out of the box, and will take care of all scrolling/resizing chores on your behalf.

Matt
  • 2,391
  • 2
  • 17
  • 18