我在 iOS 中遇到长表单问题。表单本身是用静态 TableView 完成的。对于表单,我需要一个带有 prev/next 和 done 的工具栏。在这个项目中我添加了 BSKeyboardControls ,我已经在另一个项目中自己完成了。
除非看不到下一个或上一个 textField/textView,否则工具栏工作正常。然后之前的 textField 上的焦点不会改变,我仍然在这个文本字段中输入。当我现在手动滚动并且目标下一个文本字段出现时,它神奇地成为第一响应者。这当然不是预期的行为。
所以我发现在尝试将它们设置为第一响应者之前,我必须将表格 View 滚动到相应的字段。但我无法完成这件事。
我已经用 indexPathes 尝试过了
NSIndexPath *path = [self.tableView indexPathForCell:(UITableViewCell *)textField.superview.superview];
[self.tableView scrollToRowAtIndexPat:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
虽然 super super View 是一个有效(且正确)的表格单元格,但返回的路径为零。我也尝试了一些变化
CGPoint point = [self.tableView convertPoint:textField.frame.origin toView:self.tableView];
NSIndexPath *path = [self.tableView indexPathForRowAtPoint:point];
以及 rects 和 indexPathsForRowsInRect 的其他变体。
唯一的方法是手动跟踪 textFields 单元格的索引路径,但在这种情况下这不是合适的解决方案。
有人知道如何在 tableView 中滚动到看不见的 textField 吗?
编辑:
由于 Justin Paulson 请求了 keyboardControlsPreviousNextPressed 的代码:
- (void)keyboardControlsPreviousNextPressed:(BSKeyboardControls *)controls withDirection:(KeyboardControlsDirection)direction andActiveTextField:(id)textField
{
NSIndexPath *path = [self.tableView indexPathForCell:(UITableViewCell *)((UIView *)textField).superview.superview];
NSLog(@"path %@ for cell %@", path, ((UIView *)textField).superview.superview);
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
[textField becomeFirstResponder];
}
正如我所说,根据日志的输出,路径为 nil,而 super-superview 是右 TableView 单元格
最佳答案
感谢您试图帮助我,我自己有一个解决方案。在我的 keyboardControlsPreviousNextPressed
我现在有
[self.tableView scrollRectToVisible:((UIView *)textField).superview.superview.frame animated:YES];
[textField becomeFirstResponder];
这个解决方案似乎工作得很好。我遇到的唯一问题是,有时 新获得焦点的 textField 被键盘隐藏,因此 tableViewController 不会自动调整自身。在大多数情况下,您还可以将该行滚动到顶部来避免这种情况。
NSIndexPath *path = [self.tableView indexPathForCell:(UITableViewCell *)textField.superview.superview];
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
关于iOS滚动到 View 文本字段之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11121511/