0

我试图让用户能够使用箭头键循环浏览表格视图中的所有文本字段(每个单元格两个文本字段)。

目前我正在做的是为每个文本字段设置一个标签,并将每个文本字段添加到一个数组中。一旦用户单击文本字段开始编辑它,就会出现一些箭头按钮,当他们按下箭头键时,我抓住当前选定文本字段的标签,将其用作数组的位置,拉出他们的文本字段想去,并将新的文本字段设置为第一响应者。

然而,问题在于我的细胞被重复使用。因此,如果用户将单元格滚动到屏幕外,当它返回时,表格第 1 行中的单元格可能包含带有标签 15 和 16 的文本字段,而这些文本字段数组的末尾会破坏我的箭头键。他们滚动得越多,文本字段越乱。

是否有可能在保持可重复使用的细胞的同时完成我想做的事情?或者这只是要求我不要重复使用它们?

这是我的箭头代码...

- (void)arrowPressedHandler:(UIButton *)button
{
UITextField *newTextFieldSelection;

//tags are offset by 2 because I have use tag 1 for something else, and tag 0 cannot be used
int realLocation = selectedTextField.tag - 2; 

//arrow code.  for an up button i go back 2 slots in the array, right is + 1 in array
//etc etc 
@try{
    switch (button.tag) {
        case NumericKeyboardViewUpArrow:
            newTextFieldSelection = [textFields objectAtIndex:realLocation - 2];
            [newTextFieldSelection becomeFirstResponder];
            break;
        case NumericKeyboardViewLeftArrow:
            newTextFieldSelection = [textFields objectAtIndex:realLocation - 1];
            [newTextFieldSelection becomeFirstResponder];
            break;
        case NumericKeyboardViewRightArrow:
            newTextFieldSelection = [textFields objectAtIndex:realLocation +1];
            [newTextFieldSelection becomeFirstResponder];
            break;
        case NumericKeyboardViewDownArrow:
            newTextFieldSelection = [textFields objectAtIndex:realLocation + 2];
            [newTextFieldSelection becomeFirstResponder];
            break;
        default:
            break;
    }
}
@catch (NSException *e)
{
    return;
}
}
4

1 回答 1

0

我会采取稍微不同的方法。

与其给出所有 textField 的不同标签,不如给它们一个在每一行中唯一的标签,但在行之间是相同的。即左边的得到标签1000,右边的得到1001。然后,使用单元格的indexPath 得到适当的行。您的arrowPressedHandler:方法看起来像这样(在伪代码中):

- (void)arrowPressedHandler:(UIButton *)button
{
    NSIndexPath *selectedIndexPath = indexPathOfSelectedTextfield;
    UITextField *newTextFieldSelection;
    NSIndexPath *newIndexPath;
    NSUInteger newTag = 0;

    @try{
        switch (button.tag) {
            case NumericKeyboardViewUpArrow:
                // Move back two textFields (which would be the one directly above this one)
                newIndexPath = selectedIndexPath - 1;
                newTag = selectedTextField.tag;
                break;
            case NumericKeyboardViewLeftArrow:
                // Move back one textField.
                if (selectedTextField.tag == 1000) {
                    // Selected text field is on left.  Select right textfield in the row above
                    newIndexPath = selectedIndexPath - 1;
                    newTag = 1001;
                } else {
                    // Selected text field is on right.  Select the left textfield in the same row
                    newIndexPath = selectedIndexPath;
                    newTag = 1000;
                }
                break;
            // etc.
        }
        [self.tableview scrollToRowAtIndexPath:newIndexPath];
        newTextFieldSelection = [[self.tableview cellForRowAtIndexPath:newIndexPath] viewWithTag:newTag];
        [newTextFieldSelection becomeFirstResponder];

    }
    @catch (NSException *e)
    {
        return;
    }

}
于 2013-02-22T15:42:46.433 回答