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

发布于 2024-10-20 17:49:31 字数 205 浏览 4 评论 0原文

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

I am using this other SO answer for adding UITextFields to my UITableViewCells. However, I am now at a loss how I can let the user focus on the next item by using the next button. Any hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剧终人散尽 2024-10-27 17:49:31

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

Try assigning a tag to each UITextField. Once the Next button is pressed, calculate the next tag and use [[UIView viewWithTag:nextTag] becomeFirstResponder] to change the focus to the next text field.

天煞孤星 2024-10-27 17:49:31

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

#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;
}

This worked well for me, would have to be tweaked to fit your needs though:

#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;
}
旧街凉风 2024-10-27 17:49:31

除了 Anh 发布的关于使用标签值查找下一个字段并将其设为第一个响应者之外...

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

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

但是,这会创建一个竞争条件,当您调用BecomeFirstResponder时,单元格还不可见,因此我会延迟becomeFirstResponder,直到它可见。我会在我的类中设置一个值作为属性,例如应该成为第一响应者的字段的行号,然后在 cellForRowAtIndexPath 或 willDisplayCell:forRowAtIndexPath 中设置一个值,当我要显示该单元格时,我会调用变成FirstResponder文本字段 THEN... 因为这样它就保证存在。

In addition to what Anh posted about using the tag value to find the next field and make it firstResponder...

If you are in a UITableView, you also need to keep in mind that the next UITextField might not be on the screen or even in the view because it might be below the bottom of the screen. You might need to scroll that new row into view before making it first responder. What I did in a previous app to handle this was make the tag be a value that I could use to imply the NSIndexPath of the row so I could figure out which row it was in. Then you can scroll that row into view with:

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

However, this creates a race condition where the cell isn't visible yet when you call becomeFirstResponder, so I would delay the becomeFirstResponder until it is visible. I would set a value as a property in my class, like the row number, of the field which was supposed to become first responder and then in either cellForRowAtIndexPath or willDisplayCell:forRowAtIndexPath when I'm about to display that cell I would call becomeFirstResponder on the textfield THEN... because then it is guaranteed to exist.

海风掠过北极光 2024-10-27 17:49:31

你愿意吗..

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

Would do you like ..

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];
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文