【问题标题】:Set focus to UITextField in tableCell将焦点设置到 tableCell 中的 UITextField
【发布时间】:2012-02-11 14:43:51
【问题描述】:

我正在使用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


    【解决方案1】:

    [field resignFirstResponder]; 更改为[field becomeFirstResponder];

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

    【讨论】:

    • 什么都没发生,我认为是这样,因为我设置了 tblView.allowsSelection = NO;而这个事件并没有发生。但是如果我设置 tblView.allowsSelection = YES;它只选择单元格 =(
    • 我尝试实现这种方法,但这通常不起作用。尝试将 UITextField 分配给 [textField becomeFirstResponder] 必须将选择设置为启用或设置为 YES/true。
    【解决方案2】:

    我在这里遇到了同样的问题,即使正确使用了[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];
    }
    

    它对我来说就像一个魅力。

    【讨论】:

      【解决方案3】:

      斯威夫特 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()
              }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-04
        • 2015-09-19
        • 1970-01-01
        • 1970-01-01
        • 2011-11-11
        • 1970-01-01
        • 2021-02-24
        • 2013-06-14
        相关资源
        最近更新 更多