【问题标题】:Navigating through UITextFields in different UITableViewCells when hitting next/done点击下一个/完成时在不同的 UITableViewCells 中导航 UITextFields
【发布时间】:2013-01-25 19:20:12
【问题描述】:

我尝试了this thread 中的一些解决方案,但我遇到了麻烦。我的表格动态加载了来自 plist 的数据,因此我无法在情节提要中创建从一个单元格到另一个单元格的连接。我实现了一个名为 DSCell 的自定义 UITableViewCell 类,它在单元格的右侧有两个 DSTextField 对象。在最左侧的 DSTextField 上按 Enter 时,它成功地将焦点转移到下一个字段。但是,当在右侧文本字段上按 Enter 时,它应该将焦点移动到下一个单元格中的文本字段(向下一行)。但事实并非如此。

单元格中的文本字段具有标签 2 和 3。

这是我的 cellForRowAtIndex 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"PaperCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
NSString *text = [_paper objectAtIndex:indexPath.row];
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = text;


// Set the "nextField" property of the second DSTextfield in the previous cell to the first DSTextField
// in the current cell
if(indexPath.row > 0)
{
    DSCell *lastcell = (DSCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section]];
    DSTextField *lastField = (DSTextField *)[lastcell viewWithTag:3];
    DSTextField *currentField = (DSTextField *)[cell viewWithTag:2];
    lastField.nextField = currentField;
}

return cell;

}

这里是 textFieldShouldReturn 方法:

- (BOOL) textFieldShouldReturn:(UITextField *) textField {

DSTextField *field = (DSTextField *)textField;

UIResponder *responder = field;
[responder resignFirstResponder];

responder = field.nextField;
[responder becomeFirstResponder];

return YES;

}

目前,当调用 cellForRowAtIndexPath 时,我正在尝试将第二个 DSTextField 的 nextField 属性设置为当前单元格,但它似乎不起作用。我从第 1 行开始并尝试检索上一行中的单元格,然后将最右侧文本字段的 nextField 属性分配给当前单元格中最左侧的文本字段。

有没有更好的方法来做到这一点?我不想为每个单独的文本字段使用不同的标签并这样做,这可能会变得混乱。

【问题讨论】:

    标签: ios objective-c xcode uitableview uitextfield


    【解决方案1】:

    我建议您只尝试在 textFieldShouldReturn: 方法中找到正确的单元格以将焦点转移到。可能导致您出现问题的一件事是您可能请求单元格为不可见的 lastCell,然后由 tableview 处理(因此 nextField 无效)。

    改变事物返回时发生的逻辑(您仍然希望在连续的两个单元格之间设置nextField):

    - (BOOL) textFieldShouldReturn:(UITextField *) textField {
    
    //This isn't necessary: UIResponder *responder = field;
    //Or this: [responder resignFirstResponder];
    
    //Check if it's the left or right text field
    if (textField.tag == 3) {
        //Find the cell for this field (this is a bit brittle :/ )
        UITableViewCell *currentCell = textField.superview.superview;
        NSIndexPath *ip = [self.tableView indexPathForCell:currentCell];
        if (ip.row < [self.tableView numberOfRowsInSection:ip.section] - 1) {
            DSCell *nextCell = (DSCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:ip.row+1 inSection:ip.section]];
            [[nextCell viewWithTag:2] becomeFirstResponder];
        }
    }
    
    return YES;
    
    }
    

    【讨论】:

    • 您可能还需要滚动屏幕上的内容。自从我做了很多表单样式的 UI 以来已经有一段时间了:P
    • 嗯,很有趣。现在,我只是想在第一个单元格上按 Enter,所以我什至没有尝试滚动。如何防止调用不存在的单元格(行 + 1 在最后一行时超出范围)?另外,为什么它是 textField.superview.superview 而不是一个 superview 调用?不过,我现在就试试你的建议,谢谢。
    • @Eric (ip.row contentView 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    相关资源
    最近更新 更多