TulipeMoutarde.be   About

Scroll UITableView to currently selected UITextField

Written on May 20, 2013.

Situation

I have a UITableView. Each cell of the table view has a UITextField in its contentView. The table view is actually a giant form.

In iOS when a tableView has cells containing textfield, it is supposed to scroll when a textfield becomes the first responder so focused textfield is visible when the user edits its content.

It wasn’t the case for me.

Solution

Do not forget the [textField resignFirstResponder] when passing the isFirstResponder to the next textfield. Otherwise the tableView won’t scroll to the new first responder when editing.

{% highlight objective-c %} - (BOOL)textFieldShouldReturn:(UITextField )textField { NSInteger nextTag = textField.tag + 1; UIView nextResponder = [self.view viewWithTag:nextTag]; [textField resignFirstResponder];

if (nextResponder) { [nextResponder becomeFirstResponder]; } return YES; }

{% endhighlight %}

Et voilà, the tableView will scroll as you expect.