如何在UITableView中激活下一个UITextField?

我正在使用其他的答案将UITextField添加到我的UITableViewCell 。 但是,我现在不知道如何通过使用下一个按钮让用户专注于下一个项目。 任何提示?

尝试为每个UITextField分配tag 。 按下Next按钮后,计算下一个标签并使用[[UIView viewWithTag:nextTag] becomeFirstResponder]将焦点更改为下一个文本字段。

这对我来说效果很好,但必须进行调整以满足您的需求:

 #pragma mark - UITextField Delegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { HRCFormCell * currentCell = (HRCFormCell *) textField.superview.superview; NSIndexPath * currentIndexPath = [self.tableview indexPathForCell:currentCell]; if (currentIndexPath.row != [self.questionsArray count] - 1) { NSIndexPath * nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:0]; HRCFormCell * nextCell = (HRCFormCell *) [self.tableview cellForRowAtIndexPath:nextIndexPath]; [self.tableview scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; [nextCell.textField becomeFirstResponder]; } return YES; } 

除了Anh发布的关于使用标记值查找下一个字段并使其成为firstResponder的内容…

如果您在UITableView中,还需要记住,下一个UITextField可能不在屏幕上,甚至在视图中,因为它可能位于屏幕底部。 在创建第一个响应者之前,您可能需要将该新行滚动到视图中。 我在之前的应用程序中处理此问题的方法是使标记成为一个值,我可以用它来暗示行的NSIndexPath,这样我就可以找出它所在的行。然后你可以将该行滚动到视图中:

 [tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

但是,这会创建一个竞争条件,当您调用becomeFirstResponder时,单元格仍然不可见,所以我会延迟becomeFirstResponder直到它可见。 我会设置一个值作为我的类中的属性,如行号,应该成为第一响应者的字段,然后在cellForRowAtIndexPath或willDisplayCell:forDowplayCell:forRowAtIndexPath当我要显示那个单元格时我将调用isFirstResponder那么文本字段……因为它确保存在。

你想要..

 NSArray *arrayCells = [self.aTableView visibleCells]; UITableViewCell * currentCell = (UITableViewCell *) textField.superview.superview; NSIndexPath * currentIndexPath = [self.aTableView indexPathForCell:currentCell]; if ((currentIndexPath.row != [arrayCells count] - 1) && (currentIndexPath.row < [arrayCells count]-1)) { NSIndexPath * nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:0]; UITableViewCell * nextCell = (UITableViewCell *) [self.aTableView cellForRowAtIndexPath:nextIndexPath]; [self.aTableView scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; for (id object in nextCell.contentView.subviews) { if ([object isKindOfClass:[UITextField class]]) { UITextField *tf = object; [tf becomeFirstResponder]; } }