私は自分の s に sを追加するためにthis other SO answerを使用しています。ただし、次のボタンを使用してユーザーが次の項目に集中できるようにする方法がわかりません。ヒントはありますか?UITextField
UITableViewCell
4 に答える
tag
各 UITextField に を割り当ててみてください。[次へ] ボタンが押されたら、次のタグを計算[[UIView viewWithTag:nextTag] becomeFirstResponder]
し、次のテキスト フィールドにフォーカスを変更するために使用します。
これは私にとってはうまくいきましたが、ニーズに合わせて調整する必要があります:
#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
HRCFormCell * currentCell = (HRCFormCell *) textField.superview.superview;
NSIndexPath * currentIndexPath = [self.tableview indexPathForCell:currentCell];
if (currentIndexPath.row != [self.questionsArray count] - 1) {
NSIndexPath * nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:0];
HRCFormCell * nextCell = (HRCFormCell *) [self.tableview cellForRowAtIndexPath:nextIndexPath];
[self.tableview scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
[nextCell.textField becomeFirstResponder];
}
return YES;
}
タグ値を使用して次のフィールドを検索し、それを firstResponder にすることについて Anh が投稿した内容に加えて...
UITableView を使用している場合は、次の UITextField が画面の下部にある可能性があるため、画面またはビューに表示されない可能性があることにも注意する必要があります。最初の応答者にする前に、その新しい行をスクロールして表示する必要がある場合があります。これを処理するために以前のアプリで行ったことは、タグを行の NSIndexPath を暗示するために使用できる値にすることでした。これにより、その行がどの行にあるかを把握できました。次に、その行をスクロールしてビューに表示できます。
[tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
ただし、これにより、becomeFirstResponder を呼び出したときにセルがまだ表示されていないという競合状態が発生するため、表示されるまで becomeFirstResponder を遅らせます。行番号のように、最初のレスポンダーになるはずだったフィールドのプロパティとして値を設定し、そのセルを表示しようとしているときに cellForRowAtIndexPath または willDisplayCell:forRowAtIndexPath に値を設定します。テキストフィールドTHEN ...存在することが保証されているためです。
欲しいですか ..
NSArray *arrayCells = [self.aTableView visibleCells];
UITableViewCell * currentCell = (UITableViewCell *) textField.superview.superview;
NSIndexPath * currentIndexPath = [self.aTableView indexPathForCell:currentCell];
if ((currentIndexPath.row != [arrayCells count] - 1) && (currentIndexPath.row < [arrayCells count]-1)) {
NSIndexPath * nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:0];
UITableViewCell * nextCell = (UITableViewCell *) [self.aTableView cellForRowAtIndexPath:nextIndexPath];
[self.aTableView scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
for (id object in nextCell.contentView.subviews) {
if ([object isKindOfClass:[UITextField class]]) {
UITextField *tf = object;
[tf becomeFirstResponder];
}
}