Для перехода к следующему текстовому файлу в ячейке таблицы - PullRequest
1 голос
/ 21 мая 2011

В моем приложении есть табличное представление с 2 текстовыми полями на ячейку. Назначаю теги для каждого текстового поля. При нажатии кнопки «Готово» я хочу сделать следующее текстовое поле первым респондентом (может быть между ячейками таблицы). Но код, который я сейчас использую, не работает. прикрепление настоящего кода

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

        }

    }

}

Я не получаю текстовые поля в массиве вспомогательного представления. Я использую пользовательские ячейки для ячеек таблицы. Пожалуйста, помогите.

Ответы [ 3 ]

1 голос
/ 22 мая 2011

С небольшими изменениями в коде Карима вы можете получить доступ к следующим текстам:

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

Надеюсь, это поможет.

1 голос
/ 22 мая 2011

Определите метод, как указано ниже -

- (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];
0 голосов
/ 21 мая 2011

Поскольку вы используете тег для идентификации вашего текстового обзора.Другим способом может быть использование делегатов текстового представления.

- (void)textViewDidEndEditing:(UITextView *)textView {
int currentTag = textView.tag;
int nextTag = currentTag + 1; // or something else
UITextView *nextTV = [self.tableView viewWithTag: nextTag];
[nextTV becomesFirstResponder];
}
...