У меня есть UITableView
с x количеством ячеек. В некоторых ячейках есть номер x UITextFields
. Я хочу, чтобы следующая кнопка на клавиатуре выделяла следующий текстовый экран. Я получил его работу в одной и той же ячейке, но не тогда, когда он должен перейти к текстовому полю в другой ячейке. Первый textField в каждой ячейке имеет тег 0 и второй тег 1 и т. Д. Это мой код:Перейти к следующему UITextField в разных UITableViewCells
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
// Fetch current cell
UITableViewCell *cell = (UITableViewCell *)textField.superview.superview;
// Tag for next textField
NSInteger nextTag = textField.tag + 1;
UITextField *nextTextField;
for(UITextField *subview in cell.contentView.subviews){
if([subview isKindOfClass:[UITextField class]]){
if(subview.tag == nextTag){
nextTextField = subview;
break;
}
}
}
if(nextTextField){
// Go to next textField
[nextTextField becomeFirstResponder];
} else{
// IndexPath for current cellen
NSIndexPath *indexPath = [travellersTableView indexPathForCell:cell];
UITableViewCell *nextCell;
// While a cell exist at indexPath
while([travellersTableView numberOfSections] > indexPath.section+1){
nextCell = [self tableView:travellersTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section+1]];
if(nextCell){
for(UITextField *subview in nextCell.contentView.subviews){
if([subview isKindOfClass:[UITextField class]]){
if(subview.tag == 0){
NSLog(@"%@", subview.placeholder);
nextTextField = subview;
break;
}
}
}
// Go to next textField or cell
if(nextTextField){
[nextTextField becomeFirstResponder]; NSLog(@"Next!");
break;
} else{
indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section+1];
}
}
}
if(!nextTextField){ [textField resignFirstResponder]; }
}
return NO;
}
ведут себя NSLog
«s, как ожидалось. У меня есть разные заполнители в текстовых полях, а следующий - в эхом. Следующий! также эхом, так что он пытается becomeFirstResponder
.
Возможно ли, чтобы теги увеличивались от предыдущего значения, а не возвращались к нулю? –
Короткий ответ - нет. Я использую теги таким образом, что мне нужно, чтобы они начинались с 0. –
Но все же, похоже, это работает так, как я это сделал. Он находит правильный textField, но по какой-то причине он не выделяет его. –