tulipemoutarde.be

Software Developer

Smalltalk, Swift, Java, Obj-C

@fstephany | Email | LinkedIn

I'm currently working at Ta Mère SCRL a small development shop in Belgium.

We mainly do mobile apps (iOS, Android) for clients. We also have a strong interest in Pharo Smalltalk and Rust.

logo Ta Mère SCRL

Scroll UITableView to currently selected UITextField

21 May 2013 in objective-c uitableview uitextfield tip 

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.

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

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

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