2

I have been playing with UITableView for some time and upon hitting the keyboard and scrolling issue, I decided to solve it by using the UITableViewController and take advantage of the automatically scrolling when the keyboard shows up. Unfortunately, this does not occur for me and I can not figure out why, I have tried many things and give up to ask the community.

Here is what I have so far:

  1. A regular UIViewController handling a UIView (MyView.xib) (this is also the root view)
  2. A regular UITableViewController handling a UITableView in a separate XIB file (MyTable.xib)
  3. A regular UITableViewCell has been dragged into (MyTable.xib) and resides side by side with the UITableView. This UITableViewCell has no custom class. Also there is an outlet connected to UITableViewController class.
  4. A regular UITextField sits under the UITableViewCell
  5. I have associated the file's owner identity in MyTable.xib to the UITableViewController class
  6. I have connected the delegate and datasource of the UITableView to the file's owner

This is how it looks in IB in MyTable.xib

  • UITableView
  • UITableViewCell
    • UITextField

So when I generate my table rows in the UITableViewController, I dequeue the first 5 rows to be regular cells and for the last row I return my outlet "myCell" (UITableViewCell). This is the one that has the UITextField.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    if (indexPath.row == 5) return myCell;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Test"];

    return cell;
}

Finally, in my ViewController's viewDidLoad, I create an instance of the UITableViewController and display it. It successfully shows the 5 rows and my custom one at the bottom. All in the main view, but ...

- (void)viewDidLoad
{
[super viewDidLoad];

mtc = [[MortgageTableController alloc] initWithNibName:@"MyTable" bundle:nil];
mtc.view.frame = CGRectMake(0, 44, 320, 375);
[self.view addSubview:mtc.view];
}

The problem I'm having is, if I click on the UITextField, I don't see the table automatically being scrolled in view. I thought this was one of the advantages of using UITableViewController or I maybe I'm doing something wrong here.

I also tried selecting and/or scrolling the table to the row but with no luck.

Michael
  • 7,348
  • 10
  • 49
  • 86
bendigi
  • 590
  • 6
  • 15
  • Asked and answered. http://stackoverflow.com/questions/5265559/get-uitableview-to-scroll-to-the-selected-uitextfield-and-avoid-being-hidden-by – dbrajkovic Mar 02 '12 at 05:33

3 Answers3

12

You need to call super viewWillAppear to make the scrolling automatically.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // your code
}

EDIT: Jul 19, the precondition for this to work is that your controller is a subclass of UITableViewController

Songlin
  • 372
  • 3
  • 8
1

As you are embedding the tableView in a superView, you don't get the typical behavior of a table view controller. What happens with a talble view controller when it is controlling a full screen tableView (for example pushed on a navigation controller), is the table view is resized when keyboard appears.

As what you have mainly is a view controller, you have to do the work: add your view controller or your table view controller as keyboard notification observer and scroll your table view to the position you need.

Gabriel
  • 3,319
  • 1
  • 16
  • 21
  • 1
    Thank you for your response. That's a very surprising design. What you are saying is that once I add a TableView to the subview of a UIViewController (the parent), even though all it's functionality of the view is controller by UITableViewController (the child), I do not get the child's interaction behavior ? So pushing a tableView on a Navigation Controller is different than pushing it on the parent UIViewController ? I have tried your suggestion but the scrolling appears to be a littler choppier than I liked, so I wanted to try the build in function of the UITableViewController. – bendigi Mar 02 '12 at 18:08
  • I had this problem too, this helped a lot: 'TPKeyboardAvoiding' https://github.com/michaeltyson/TPKeyboardAvoiding – Marc Apr 10 '12 at 09:54
  • Is it true that adding a tableview controller won't autoscroll when you put it on a uiviewcontroller?. Do you have any reference which states that?.For me the auto scrolling works in my sample project but not in my real project, Curious to know the nuances. – Vignesh Sep 03 '12 at 09:24
0

I ran into a similar issue but I was using a tableviewcontroller so I should have gotten the tableview offsetting behavior for free. The issue turned out to be that I was calling [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardDidShowNotification object:nil];. I believe this caused the tableviewcontroller to not receive this notification (I'm thinking maybe an object can only registered 1 method per notif). Changing the above to [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardWillShowNotification object:nil]; solved my issue.

Tylerc230
  • 2,883
  • 1
  • 27
  • 31