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

15

我正在使用这个SO答案来给我的UITableViewCell添加UITextField。然而,我现在不知道应该如何让用户通过使用下一个按钮聚焦到下一个项目。有什么提示吗?

4个回答

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

12
如果单元格不在屏幕上,可能需要先将下一个indexPath可见,因为该单元格可能尚不存在。例如:[self.myTableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; - Bogatyr
谢谢,听起来很合理。 - Arne
它应该是 becomeFirstResponder,而不是 becomes。 - shim
@FabianoFrancesconi 什么答案? - Bryan Bryce

12

这对我很有效,但需要根据您的需求进行微调:

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

感谢提供代码示例,为我的工作提供了很好的基础。虽然我不得不使用 UITableViewScrollPositionTop - Jason McCreary
之前的代码怎么样了?这段代码好像不起作用。但是我修改了一些代码,比如: if (isNextClicked==YES) { nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:0]; }else{ nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row - 1 inSection:0]; } - karthikeyan
它的工作原理是向前和向后移动,直到最后一行,如果我在最后一行,它就不会回到上一行,导致程序崩溃。 - karthikeyan

10

除了Anh提到使用标签值来查找下一个字段并使其成为第一响应者之外...

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

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

然而,这会创建一个竞争条件,即当您调用becomeFirstResponder时,单元格尚未可见,因此我会延迟becomeFirstResponder直到它可见为止。我会在我的类中设置一个值作为属性,例如字段的行号,该字段应成为第一响应者,然后在要显示该单元格的cellForRowAtIndexPath或willDisplayCell: forRowAtIndexPath中,当我将要显示该单元格时,我会在textfield上调用becomeFirstResponder,因为此时它保证已经存在。


好的,我会先尝试Anh的答案,然后再实现你的扩展(因为我现在只有很少的单元格)。 - Arne
1
只需使用此方法来处理竞态条件-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{ - MobileMon
另外需要注意的是,不能依赖cellForRowAtIndexPath方法来获取下一个/上一个单元格的文本字段,因为如果它不在视图中,那么该单元格不存在。一种解决方案是保存要前往的索引路径,然后在didendscrollinganimation方法中检索所需的文本字段。 - shim

2

你想要做什么?

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接