我有一个 tableView,允许用户使用一些箭头键导航文本字段。但是,当用户位于表格顶部时,必须按下箭头按钮两次,然后第一响应者才会真正改变。第一次单击显示下一个单元格,第二次单击实际上将焦点移动到上面单元格中的 textField。所有其他方向和箭头都可以在焦点更改和滚动发生的地方单击。用户位于底部时除外。同样的事情也会发生。(但是用户不能这样做,因为当用户选择文本字段时我将滚动设置为顶部)
有没有办法即使在桌子的顶部也能获得相同的视觉效果?
我的代码...
switch (arrowButton.tag) {
case UpArrow:
destinationIndex = [NSIndexPath indexPathForRow:currentIndex.row - 1 inSection:currentIndex.section];
newTag = currentTextField.tag;
break;
case LeftArrow:
// Move back one textField.
if (currentTextField.tag == CustomerDetailsOrderViewControllerCellLeftTextField) {
// Selected text field is on left. Select right textfield in the row above
destinationIndex = [NSIndexPath indexPathForRow:currentIndex.row - 1 inSection:currentIndex.section];
newTag = CustomerDetailsOrderViewControllerCellRightTextField;
} else {
// Selected text field is on right. Select the left textfield in the same row
destinationIndex = currentIndexPath;
newTag = CustomerDetailsOrderViewControllerCellLeftTextField;
}
break;
case RightArrow:
// Move forward one textField.
if (currentTextField.tag == CustomerDetailsOrderViewControllerCellLeftTextField) {
// Selected text field is on Left. Select right textfield
destinationIndex = currentIndexPath;
newTag = CustomerDetailsOrderViewControllerCellRightTextField;
} else {
// Selected text field is on right. Select the left textfield in the row below
destinationIndex = [NSIndexPath indexPathForRow:currentIndex.row + 1 inSection:currentIndex.section];
newTag = CustomerDetailsOrderViewControllerCellLeftTextField;
}
break;
case DownArrow:
destinationIndex = [NSIndexPath indexPathForRow:currentIndex.row + 1 inSection:currentIndex.section];
newTag = currentTextField.tag;
break;
default:
break;
}
[_tableView scrollToRowAtIndexPath:destinationIndex atScrollPosition:UITableViewScrollPositionNone animated:YES];
newTextFieldSelection = (UITextField *)[[_tableView cellForRowAtIndexPath:destinationIndex] viewWithTag:newTag];
[newTextFieldSelection becomeFirstResponder];