Scroll UITableView to currently selected UITextField
Written on May 20 2013.
This blog post was written a long time ago and may not reflect my current opinion or might be technically out of date. Read with a grain of salt.
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.