繁体   English   中英

当点击next / done时在不同的UITableViewCells中浏览UITextField

[英]Navigating through UITextFields in different UITableViewCells when hitting next/done

我已经尝试了该线程中的一些解决方案,但是遇到了麻烦。 我的表中动态加载了plists的数据,因此无法在情节提要中创建从一个单元格到另一个单元格的连接。 我实现了一个名为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属性分配给当前单元格中最左边的文本字段。

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

我建议您仅尝试在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;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM