0

ユーザーがいくつかの矢印キーでテキストフィールドをナビゲートできるようにするtableViewがあります。ただし、ユーザーがテーブルの一番上にいる場合、ファーストレスポンダーが実際に変更される前に、矢印ボタンを 2 回押す必要があります。最初のクリックで次のセルが表示され、2 回目のクリックで実際には上のセルの 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 に答える