UITableView having one Cell with multiple UITextFields (some of them in one row) scrolling crazy

Posted by Allisone on Stack Overflow See other posts from Stack Overflow or by Allisone
Published on 2010-06-09T18:03:00Z Indexed on 2010/06/09 18:12 UTC
Read the original article Hit count: 574

I have a UITableView (grouped). In that tableview I have several UITableViewCells, some custom with nib, some default. One Cell (custom) with nib has several UITextfields for address information, thus also one row has zip-code and city in one row.

When I get the keyboard the tableview size seems to be adjusted automatically (vs. another viewController in the app with just a scrollview where I had to code this functionality on my own) so that i can scroll to the bottom of my tableview (and see it) even though the keyboard is up. That's good.

BUT when I click on a textfield the tableview gets either scrolled up, or down, I can't figure out the logic. It seems to be rather random up/down scrolling / contentOffset setting.

So I have bound the Editing Did Begin events of the textfields to a function that has this code.

- (IBAction)textFieldDidBeginEditing:(UITextField *)textField { 
  CGPoint pt;
  CGRect rc = [textField bounds];
  rc = [textField convertRect:rc toView:self.tableView];
  pt = rc.origin;
  pt.x = 0;
  [self.tableView setContentOffset:pt animated:YES];        
  ...
}

This, well, it seems to work most of the time,

BUT it doesn't work if I click the first textfield (the view jumps so that the second row gets to the top and the first row is out of the current visible view frame) AND it also doesn't work if I first select the zip textfield and next the city textfield (both in one row) or vice versa. If I do so, the tableview seems to jump to the (grouped tableview) top of my viewForHeaderInSection(this section with this mentioned cell with all my textfields)

What is is going on ? Why is this happening ? How to fix this ?

Edit This on the other hand behaves as expected (for the two Textviews wit same origin.y)

if (self.tableView.contentOffset.y == pt.y) {
  pt.y = pt.y + 1;
  [self.tableView setContentOffset:pt animated:YES];        
}else {
  [self.tableView setContentOffset:pt animated:YES];                
}

But this is a stupid solution. I wouldn't like to keep it that way. And this also doesn't fix the wrong jumping, when clicking the first textfield at first.

© Stack Overflow or respective owner

Related posts about uitableview

Related posts about keyboard