Установить фокус на UITextField в tableCell - PullRequest
24 голосов
/ 16 января 2012

Я создаю табличное представление с UITextField s динамически.

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];

И теперь я хочу установить фокус на это текстовое поле, когда пользователь касается ячейки.Я пишу это

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
    UITextField* field = (UITextField *) [tblView viewWithTag: newIndexPath.row];
    [field resignFirstResponder];
}

Но это не работает

Ответы [ 3 ]

60 голосов
/ 16 января 2012

изменить [field resignFirstResponder]; с помощью [field becomeFirstResponder];

resignFirstResponder используется для удаления клавиатуры (фокус) из текстового поля

1 голос
/ 26 января 2016

Я сталкивался здесь с той же проблемой, даже используя [field becomeFirstResponder];.

.

Я также использовал теги, чтобы получить объекты, которые у меня есть внутри клетки. Ваш didSelectRowAtIndexPath должен выглядеть так, чтобы получить правильную ячейку:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    currentRow = indexPath.row;

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UITextField *indexField = (UITextField *)[cell viewWithTag:101];
    [indexField becomeFirstResponder];
}

Это сработало для меня как шарм.

0 голосов
/ 06 марта 2018

Swift 3

myTextField.becomeFirstResponder()

Мой код.Взять индексный путь текущей ячейки.И получите ячейку следующего indexpath.row.И называется ** cell1.txtFindings.becomeFirstResponder () **

  if  let textFieldIndexPath = specsListView.indexPath(for: cell){
        let newIndexPath = IndexPath(row: textFieldIndexPath.row + 1, section: 0)

        if let cell1: SpecsTableViewCell = specsListView.cellForRow(at: newIndexPath) as? SpecsTableViewCell{
            cell1.txtFindings.becomeFirstResponder()
        }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...