5

I have a UITableViewCell with UITextField. In the edit mode, when the keyboard comes up, the row I am trying to edit gets overlapped by the keyboard. Ideally the row should scroll up so that I can edit the cell.

I have searched stackoverflow throughly and found several different solutions. Most of them has to do with calculations to move or scroll the view up. Now there is a sample code from apple called TaggedLocations. They have the exact same behavior. And there is no code doing any complex calculations to move the view up.

I also thoroughly checked the IB interface and could not find any fancy thing going on either. If you download and run the code, it beautifully pushes up the row to a perfect position for the edit.

Does anybody know what trick is there in the TaggedLocations project which does this so elegantly? Location of the project: http://developer.apple.com/library/ios/#samplecode/TaggedLocations/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40008914

For the complex calculations I was referring, look at the following thread for example:

How to make a UITextField move up when keyboard is present?

thanks

Community
  • 1
  • 1
mbh
  • 3,302
  • 2
  • 22
  • 24

4 Answers4

1

This line in RootViewController.m is what causes the scroll:

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

Put that in your cellForRowAtIndexPath: method and it should work.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • I tried that. Thats not it. That only come into play for adding new events. If you see, the scroll works for edit as well. Also the Tag selection also has similar behavior and no such scroll code as you mentioned. Moreover it is not in the cellForRowAtIndexPath: method. – mbh May 23 '11 at 00:11
  • I'm sorry, `cellForRowAtIndexPath:` is probably not the best method to put that in. What about making your class conform UITextFieldDelegate, then in `textFieldDidBeginEditing` you somehow get the index path for the row that textfield is in, and then call the line of code above in that method? I didn't really dig into Apple's code, but "scrollToRowAtIndexPath" seems to be exactly what you want... – Scott Berrevoets May 23 '11 at 00:20
  • Sorry that is not the right code. It does not work. Even if you comment it out, you will get the same behavior. – mbh May 23 '11 at 02:02
1

I figured out the problem. Initially I had

TabBarController
- UIViewController
-- UINavigationController
--- UITableViewController

- UIViewController
- -UINavigationController
--- UITableViewController

With this kind of a hierarchy the table cells do not roll up when keyboard is displayed. But if I change the hierarchy to the following the rolling up happens just fine without any additional code!

TabBarController
- UINavigationController
-- UITableViewController

- UINavigationController
-- UITableViewController

When I duplicated the Apple code, I somehow messed up the hierarchy. Now on switching to the new hierarchy, it works fine. No additional code or calculation necessary.

mbh
  • 3,302
  • 2
  • 22
  • 24
  • I have the exact same issue, however I have the heirachy you mentioned that fixed your issue. However my issue still remains. Did you come across any other problems that could cause it to happen? –  Feb 12 '12 at 14:33
  • Thanks. your answer really helped. A small addition, if you can't eliminate UIViewController from your hierarchy you can create a UINavigationController with your tableview controller as its root and add the navigation controller's view as subview to your VC. Hope it helps some one!. – Vignesh Sep 03 '12 at 12:22
0

or call viewWillAppear if you have your own hierarchy of controllers... then table views will automatically scroll cells to be visible when keyboard appears.

andrei
  • 31
  • 1
  • 2
0

I had a similar confusion with Apple's sample code TaggedLocations, although in my case I realized that my table view was being managed by a UIViewController subclass as opposed to a UITableViewController subclass, and UITableViewController apparently manages the scrolling for you. It works quite well, so a lot of these complex calculations are unnecessary. I wish I didn't spend so much time trying to figure them out.

I don't see why the hierarchy you describe would matter. As long as the UITableViewController is managing the text fields, it should automatically take care of the scrolling to avoid the keyboard.

shim
  • 9,289
  • 12
  • 69
  • 108