テーブルセル内の次のテキストフィールドに移動するには
-
08-09-2020 - |
質問
私のアプリケーションでは、セルごとに2つのテキストフィールドを持つテーブルビューがあります。テキストフィールドごとにタグを割り当てています。[完了]ボタンを押すと、次のテキストフィールドを最初のテキストフィールドにしたい(テーブルセル間になります)。しかし、私が使用しているコードは機能しません。現在のコードを添付する
NSArray *viewsToBecomeFirstResponder = [tableView subviews];
for (UIView *firstResponder in viewsToBecomeFirstResponder) {
if ([firstResponder isKindOfClass:[UITextField class]]) {
if (firstResponder.tag == nextTag ) {
[firstResponder becomeFirstResponder];
}
}
}
.
サブビュー配列のテキストフィールドを取得していません。表セルにカスタムセルを使用しています。助けてください。
解決
カリムのコードを変更すると、このようなTextViesにアクセスできます。
- (void)textViewDidEndEditing:(UITextView *)textView {
//Get the indexPath for the currently selected cell
NSIndexPath *cellPath = [(UITableView*)[self view] indexPathForSelectedRow];
//Get the actual cell
CustomTableCell *cell = (CustomTableCell *)[(UITableView*)[self view] cellForRowAtIndexPath:cellPath];
int currentTag = textView.tag;
int nextTag = currentTag + 1; // or something else
UITextView *nextTV = [cell viewWithTag: nextTag];
[nextTV becomesFirstResponder];
}
.
これが役立つことを願っています。
他のヒント
以下の方法を定義する -
- (UITextField *) nextToRespondAfterTextField:(UITextField*)textField {
NSInteger nextTag = textField.tag + 1;
CustomTableCell *theCell = (CustomTableCell*)[textField.superview];
for ( UIView *aView in theCell.subviews ) {
if ( [aView isKindOfClass:[UITextField class]]) {
if ( aView.tag == nextTag ) {
return aView;
}
}
}
NSIndexPath *indexPath = [self.tableView indexPathForCell:theCell];
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
NSInteger numberOfSections = [self.tableView numberOfSections];
if ( [self.tableView numberOfRowsInSection:section] != (row + 1) ) {
indexPath = [NSIndexPath indexPathForRow:(row + 1) inSection:section];
} else {
do {
section++;
} while ( section < numberOfSections && [self.tableView numberOfRowsInSection:section] == 0 ) ;
if ( section == numberOfSections ) {
return nil;
} else {
indexPath = [NSIndexPath indexPathForRow:0 inSection:section];
}
}
theCell = (CustomTableCell*)[self.tableView cellForRowAtIndexPath:indexPath];
if ( theCell == nil ) {
return nil;
}
for ( UIView *aView in theCell.subviews ) {
if ( [aView isKindOfClass:[UITextField class]]) {
if ( aView.tag == nextTag ) {
return aView;
}
}
}
return nil;
}
.
他の場所、
UITextField *nextField = [self nextToRespondAfterTextField:textField];
[nextField becomeFirstResponder];
. TAGを使用してTextViewを識別しているので。もう1つの方法は、テキストビューのデリゲートを使用することです。
- (void)textViewDidEndEditing:(UITextView *)textView {
int currentTag = textView.tag;
int nextTag = currentTag + 1; // or something else
UITextView *nextTV = [self.tableView viewWithTag: nextTag];
[nextTV becomesFirstResponder];
}
. 所属していません StackOverflow