gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:01:27 24 4
gpt4 key购买 nike

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

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

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

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

}

最佳答案

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

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

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

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

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

关于ios - 带有文本字段的 UiTableView - 出现键盘时滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17715162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com