Set focus to UITextField in tableCell

I am creating a table view with UITextFields dynamically.

l_textField = [[UITextField alloc] initWithFrame:TextFieldFrame];
l_textField.tag = cellRow;

l_textField.delegate = self;
l_textField.font = [UIFont boldSystemFontOfSize:14];
l_textField.textColor = [UIColor blackColor];
[l_textField setEnabled:YES];

[cell.contentView addSubview:l_textField];

And now I want to set focus on this text field when user touch cell. I write this

  • How do I register NSUndoManager in Swift?
  • Repeat image horizontally and vertically
  • IOS launch image sizes
  • Get the last redirected url in iOS 5?
  • Setting user_version in sqlite
  • In Swift, how can I change an attribute of part of a string?
  • - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
        UITextField* field = (UITextField *) [tblView viewWithTag: newIndexPath.row];
        [field resignFirstResponder];
    }
    

    But this doesn’t work

    2 Solutions Collect From Internet About “Set focus to UITextField in tableCell”

    change [field resignFirstResponder]; with [field becomeFirstResponder];

    resignFirstResponder is used to remove keyboard (focus) from text field

    I’ve faced the same issue here, even using correctly the [field becomeFirstResponder];.

    I also used tags to get the objects I have inside the cell. Your didSelectRowAtIndexPath should look like this to get the right cell:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        currentRow = indexPath.row;
    
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UITextField *indexField = (UITextField *)[cell viewWithTag:101];
        [indexField becomeFirstResponder];
    }
    

    It worked like a charm for me.