0

UITextField でキーボードを閉じる方法を知りたいのですが、Outlets を介してそれを行う方法を知っていますが、今は次のようなコードでテキストフィールドを宣言しています:

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

cell.accessoryType = UITableViewCellAccessoryNone;

UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if([indexPath row] == 0) {
    playerTextField.placeholder = @"Server Address";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
} else if([indexPath row] == 1){
    playerTextField.placeholder = @"Server Port";
    playerTextField.keyboardType = UIKeyboardTypeDecimalPad;
    playerTextField.returnKeyType = UIReturnKeyDone;
} else {
    playerTextField.placeholder = @"Password";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
    playerTextField.secureTextEntry = YES;
}

playerTextField.backgroundColor = [UIColor clearColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
playerTextField.textAlignment = UITextAlignmentLeft;
playerTextField.tag = 0;

playerTextField.clearButtonMode = UITextFieldViewModeNever;
[playerTextField setEnabled: YES];

[cell.contentView addSubview:playerTextField];


return cell;
}

どうすればそれを管理できますか?

4

2 に答える 2

2

テキストフィールドはセル内にあるため、既にタグ付けする必要がありますが、 とは異なるものを使用することをお勧めします0。その後、辞任する必要があるときはいつでも(探すべきセルがわかっていると仮定して):

    UITextField *myField = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:myRow inSection:mySection]].contentView viewWithTag:myTag];
    [myField resignFirstResponder];

どのセルかがわからない場合は、すべてのセルをループする必要があります。

お役に立てれば

于 2013-02-01T22:19:31.163 に答える
0

各セルに 1 つずつ、多くの textFields があるように見えますか?

プロパティを追加する必要があります@property (strong, nonatomic) UITextField *currentTextField

textField 作成メソッドでは、テーブル ビュー コントローラーをテキスト フィールド デリゲートとして設定する必要があります。

playerTextField.delegate = self;

次に、tableViewController に UITextFieldDelegate プロトコルを実装させ (<UITextFieldDelegate>ヘッダー ファイルのクラス名の後に追加)、このメソッドの実装を追加する必要があります。

- (void)textFieldDidBeginEditing:(UITextField *)textField {
     self.currentTextField = textField;
}

つまり、textFields の 1 つが編集を開始すると、追跡されます。

(void)saveおそらく、アクションのようなものを呼び出すイベントまたはボタンがあります。その実装に追加します。

- (void)save {
     [self.currentTextField resignFirstResponder];
}

textField がいつ編集を終了したかを追跡することもできます。(void)textFieldDidEndEditing:(UITextField *)textField

于 2013-02-01T22:18:56.153 に答える