我有UITableView
x 个单元格。在某些单元格中有 x 个数UITextFields
。我希望键盘上的下一个按钮突出显示下一个文本字段。我让它在同一个单元格中工作,但当它应该跳转到不同单元格中的 textField 时却没有。每个单元格中的第一个文本字段具有标记 0,第二个标记为 1,依此类推。这是我的代码:
- (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
行为符合预期。我在 textFields 中有不同的占位符,并回显下一个。下一个!也得到了回应,所以它正在尝试becomeFirstResponder
。