0

我有一个 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];
4

1 回答 1

0

杰西,是动画杀死了你。

尝试

    [_tableView scrollToRowAtIndexPath:destinationIndex atScrollPosition:UITableViewScrollPositionNone animated:NO];
    newTextFieldSelection = (UITextField *)[[_tableView cellForRowAtIndexPath:destinationIndex] viewWithTag:newTag];
    [newTextFieldSelection becomeFirstResponder];

    [_tableView scrollToRowAtIndexPath:destinationIndex atScrollPosition:UITableViewScrollPositionNone animated:NO];
    newTextFieldSelection = (UITextField *)[[_tableView cellForRowAtIndexPath:destinationIndex] viewWithTag:newTag];
double delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        newTextFieldSelection = (UITextField *)[[_tableView cellForRowAtIndexPath:destinationIndex] viewWithTag:newTag];
        [newTextFieldSelection becomeFirstResponder];
    });

那些又快又脏。您可以更进一步,在表格的 contentOffset 上使用 KVO,并在动画完成后执行您的 firstResponder 业务。

于 2013-02-27T20:41:41.550 回答