将焦点设置为tableCell中的UITextField

问题描述 投票:24回答:3

我正在动态地创建一个UITextFields表格视图。

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

但这不起作用

ios events uitableview uitextfield
3个回答
60
投票

[field resignFirstResponder];改变[field becomeFirstResponder];

resignFirstResponder用于从文本字段中删除键盘(焦点)


1
投票

即使正确使用[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
投票

斯威夫特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()
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.