Mik*_*e G 0 objective-c uitableview ios
我正在tableview控制器中创建一个带有textfields的表单.在输入字段值后按下返回键时,我试图在这里实现两件事:
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 ;
}
Run Code Online (Sandbox Code Playgroud)
要根据textfield的焦点滚动tableview,请尝试以下操作:
- (void) textFieldDidBeginEditing:(UITextField *)textField {
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
[yourTableView scrollToRowAtIndexPath:[yourTableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处使用滚动位置设置为顶部 UITableViewScrollPositionTop
您可能想知道为什么使用两个超级视图textfield.一个用于 UITableViewCellContentView,另一个用于,UITableViewCell以便您可以获取表的当前/聚焦单元格的实例.
它有效,我们在其中一个项目中使用过它
| 归档时间: |
|
| 查看次数: |
7735 次 |
| 最近记录: |