简体   繁体   English

带文本字段的UiTableView-出现键盘时滚动

[英]UiTableView with textfields - scroll when keyboard appears

I am creating a form with textfields in a tableview controller. 我在tableview控制器中创建带有文本字段的表单。 I am trying to achieve 2 things here when the return key is pressed after entering value for a field: 我试图在为字段输入值后按返回键来实现两件事:

1) Move cursor to next field 2) Scroll the tableview up 1)将光标移到下一个字段2)向上滚动表格视图

My code is working for moving the cursor to the next field but the scroll part is not working. 我的代码可以将光标移动到下一个字段,但是滚动部分不起作用。 Could you let me know what I am missing here. 你能告诉我我在这里想念的东西吗? I researched similar issues on stack overflow and am following suggestions from them but still facing issues. 我研究了有关堆栈溢出的类似问题,并遵循它们的建议,但仍然面临问题。 Appreciate your inputs. 感谢您的投入。

- (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 ;

}

For scrolling the tableview up as per textfield's focus try this: 为了根据文本字段的焦点向上滚动tableview,请尝试以下操作:

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

Here you can set scroll position to top by using UITableViewScrollPositionTop 在这里,您可以使用UITableViewScrollPositionTop将滚动位置设置为顶部

You may wonder why two superviews are used for textfield . 您可能想知道为什么将两个超级视图用于textfield One is for UITableViewCellContentView and another is for UITableViewCell so that you can grab the instance of your table's current/focused cell. 一个用于UITableViewCellContentView ,另一个用于UITableViewCell以便您可以获取表的当前/焦点单元格的实例。

It works, we have used this in one of our projects 它有效,我们已经在一个项目中使用了它

I used the viewcontroller as the data source for the table view. 我使用viewcontroller作为表视图的数据源。 My solution: 我的解决方案:

  1. Make the UITextField as a public property for your custom UITableViewCell. 将UITextField设置为自定义UITableViewCell的公共属性。
  2. Inside cellForRowAtIndexPath, add target to the textfield for UIControlEventEditingDidBegin control event: 在cellForRowAtIndexPath内部,将目标添加到UIControlEventEditingDidBegin控件事件的文本字段中:

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

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

You use TKKeyboardAvoidingTableview so no need to mannually scroll tableview it automatically scoll when cursor tap 您使用TKKeyboardAvoidingTableview,因此无需手动滚动tableview,当点击光标时它会自动变色

you set xib in select tableview and set custom class like this 您在选择tableview中设置xib并设置这样的自定义类

在此处输入图片说明

Try to use this code it may help you 尝试使用此代码可能会对您有所帮助

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM