2012-01-16 15 views
24

UITextFieldのテーブルビューを動的に作成しています。テーブルセルのUITextFieldにフォーカスを設定

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

書くしかし、これは私が同じ直面したテキストフィールドから

答えて

59

変更[field resignFirstResponder];を動作しません。 [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

何も起こったといい、私は設定becouse、私はそのように考えてtblView.allowsSelection = NO;そして、この出来事は起こりません。しかし、私はtblView.allowsSelection = YESを設定します。それだけではセルを選択する=( – nabiullinas

+0

このようなアプローチを実装しようとしましたが、これは通常は機能しません。 [textField becomeFirstResponder]にUITextFieldを割り当てると、選択が有効になっているか、YES/trueに設定されている必要があります。 –

1

をキーボード(フォーカス)を削除するために使用される[field becomeFirstResponder];

resignFirstResponder

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() 
     } 
} 
関連する問題