1

私のアプリケーションでは、セルごとに 2 つのテキスト フィールドを持つテーブル ビューがあります。各テキスト フィールドにタグを割り当てています。完了ボタンを押すと、次のテキストフィールドを最初の応答者にしたい(テーブルセルの間にある可能性があります)。しかし、現在使用しているコードは機能しません。プレゼントコードを付ける

    NSArray *viewsToBecomeFirstResponder = [tableView subviews];
for (UIView *firstResponder in viewsToBecomeFirstResponder) {
    if ([firstResponder isKindOfClass:[UITextField class]]) {
        if (firstResponder.tag == nextTag ) {
            [firstResponder becomeFirstResponder];

        }

    }

}

サブビュー配列でテキスト フィールドを取得できません。テーブルセルにカスタムセルを使用しています。助けてください。

4

3 に答える 3

1

Karim のコードを少し変更するだけで、次のように 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];
}

お役に立てれば。

于 2011-05-21T20:50:32.143 に答える
1

以下に示すようにメソッドを定義します–</p>

- (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];
于 2011-05-21T22:07:57.240 に答える
0

タグを使用してテキストビューを識別しているためです。もう 1 つの方法は、テキスト ビュー デリゲートを使用することです。

- (void)textViewDidEndEditing:(UITextView *)textView {
int currentTag = textView.tag;
int nextTag = currentTag + 1; // or something else
UITextView *nextTV = [self.tableView viewWithTag: nextTag];
[nextTV becomesFirstResponder];
}
于 2011-05-21T11:28:17.553 に答える