Перейти к следующему текстовому полю в таблице-ячейке

StackOverflow https://stackoverflow.com/questions/6081325

Вопрос

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

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

        }

    }

}
.

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

Это было полезно?

Решение

с небольшим модификацией кода Karim, вы можете получить доступ к тексту, как это:

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

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

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top