【问题标题】:UiTableView with textfields - scroll when keyboard appears带有文本字段的 UiTableView - 出现键盘时滚动
【发布时间】:2013-07-18 05:26:30
【问题描述】:

我正在 tableview 控制器中创建一个带有文本字段的表单。在为字段输入值后按下返回键时,我试图在这里实现两件事:

1) 将光标移动到下一个字段 2) 向上滚动表格视图

我的代码正在将光标移动到下一个字段,但滚动部分不起作用。你能告诉我我在这里缺少什么吗?我研究了有关堆栈溢出的类似问题,并遵循他们的建议,但仍然面临问题。感谢您的投入。

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    if ([textField isEqual:self.firstName]){
    //Move cursor to next field
    [self.lastName becomeFirstResponder];

    //scroll    
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

if ([textField isEqual:self.lastName]){
    //Move cursor to next field
    [self.emailId becomeFirstResponder];

    //scroll
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

if ([textField isEqual:self.emailId]){

    //Move cursor to next field
    [self.phoneNumber becomeFirstResponder];

    //scroll
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

if ([textField isEqual:self.phoneNumber]){

    //Move cursor to next field
    [self.password becomeFirstResponder];

    //scroll
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

// This is the last field hence dismiss keyboard -> This part is working
if ([textField isEqual:self.password]){
    [textField resignFirstResponder];

}

return YES ;

}

【问题讨论】:

  • 那是因为你的 ContentSize 不够大,试着增加你的 UITableView ContentSize。

标签: ios objective-c uitableview


【解决方案1】:

要根据文本字段的焦点向上滚动表格视图,请尝试以下操作:

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
    [yourTableView scrollToRowAtIndexPath:[yourTableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];   
}

在这里您可以使用UITableViewScrollPositionTop将滚动位置设置为顶部

您可能想知道为什么textfield 使用两个超级视图。一个用于UITableViewCellContentView,另一个用于UITableViewCell,以便您可以获取表格当前/焦点单元格的实例。

它有效,我们已经在我们的一个项目中使用了它

【讨论】:

  • 在 iOS7 上,UITableViewCell 上有一个额外的视图(据我所知,它用于滑动手势和删除按钮)。因此,您的解决方案不会长期有效。我建议找到一种在视图控制器上查找文本字段所有者单元格的方法(例如,遍历单元格并比较文本字段)
【解决方案2】:

我使用视图控制器作为表格视图的数据源。我的解决方案:

  1. 将 UITextField 设为自定义 UITableViewCell 的公共属性。
  2. 在 cellForRowAtIndexPath 中,将目标添加到 UIControlEventEditingDidBegin 控件事件的文本字段:

    [((YourCustomCell *) cell).textfield addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    
  3. 实现命名方法:找到文本字段的单元格并滚动到该单元格

    - (void)textFieldDidBeginEditing:(UITextField *)textField{
        for ( NSIndexPath *ip in [self.tableview indexPathsForVisibleRows] ){
            UITableViewCell * cell = [self.tableview cellForRowAtIndexPath:ip];
            if  ( [cell isKindOfClass:[YourCustomCell class]]){
    
                if ( [textField isEqual:((YourCustomCell *)cell).textfield] ){
                    [self.tableview selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionTop];
                    [self setContinueButtonEnabled:YES];
                }
            }
        }
    }
    

【讨论】:

    【解决方案3】:

    您使用TKKeyboardAvoidingTableview,因此无需手动滚动表格视图,当光标点击时它会自动滚动

    您在选择表视图中设置 xib 并像这样设置自定义类

    【讨论】:

      【解决方案4】:

      尝试使用此代码可能对您有所帮助

        CGPoint pt;
          CGRect rc = [textField bounds];
          rc = [textField convertRect:rc toView:scrView];
          pt = rc.origin;
          pt.x = 0;
          pt.y =0;
          [scrView setContentOffset:pt animated:YES];
      

      【讨论】:

        猜你喜欢
        • 2013-02-25
        • 1970-01-01
        • 1970-01-01
        • 2020-08-15
        • 2017-01-29
        • 1970-01-01
        • 2012-12-02
        • 2014-10-30
        相关资源
        最近更新 更多