【问题标题】:Navigation of text fields inside cells that are re-used重复使用的单元格内的文本字段导航
【发布时间】:2013-02-22 15:22:45
【问题描述】:

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

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

然而,问题在于我的细胞被重复使用。因此,如果用户将单元格滚动到屏幕外,当它返回时,表格第 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;
}
}

【问题讨论】:

    标签: iphone ios objective-c xcode ipad


    【解决方案1】:

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

    不要给文本字段的所有不同标签,而是给它们一个在每一行中唯一的标签,但在行之间是相同的。 IE。左边的得到标签 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;
        }
    
    }
    

    【讨论】:

    • 我明白了。我正在用索引路径折腾想法,但这比我迄今为止想到的任何东西都要好。这应该有效。我会测试一下。但是有一个问题,您将如何获得 'indexPathOfSelectedTextField' 值?
    • 它涉及更多一点(确保您不在第一行/最后一行等),但不应该太难。获取所选文本字段所在行的 indexPath 可能有点棘手。看看这个答案,了解我过去发布的一个很好的解决方案(第 3 条):stackoverflow.com/a/14352372/937822
    • 大声笑,以这种方式获取索引路径看起来有点骇人听闻,但似乎没有任何优雅的解决方案。感谢您的帮助!
    • 嗯,这正是 tableview 方法的设计目的,所以我不会称之为 hackey。 :) 它工作得很好,我发现的唯一其他解决方案要么要求您访问 textview 的父级以查找单元格(脆弱),要么您处理单元格的重用,这会导致许多极端情况由于滚动而必须处理的问题(如您所见)。
    • 是的,我觉得你必须在你的对象上画一个矩形很奇怪。但这就是该方法所接受的,所以我猜它就是它的完成方式。
    猜你喜欢
    • 1970-01-01
    • 2015-02-02
    • 2019-10-27
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多