在Top时,必须调用scrollToRowAtIndexPath两次

时间:2013-02-26 21:30:14

标签: iphone ios objective-c ipad cocoa-touch

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

1 个答案:

答案 0 :(得分: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业务。