当我的 tableView 被编辑时,我有一个带有插入附件的行,它在它上面插入一个新的 table view 单元格,它有一个文本字段。如何使文本字段处于活动状态(即成为FirstResponder)?例如,在 Apple 联系人应用程序中,如果您添加新手机,它会自动选择新行的文本字段并出现键盘。
从网上搜索,似乎单元格的 textField 并没有立即添加到队列中,因此您必须在正确的时间调用 becomeFirstResponder 。我应该什么时候调用它?
在 commitEditingStyle: forRowAtIndexPath 我试过:...
else if (editingStyle == UITableViewCellEditingStyleInsert) {
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
MyCustomCell* cell = [tableView cellForRowAtIndexPath: indexPath];
[cell.textField becomeFirstResponder];
}
不工作。
在 cellForRowAtIndexPath 我也尝试过(有点破解):
if (cell.textField.text.length == 0) {
[conditionCell.textField becomeFirstResponder];
}
但它似乎也没有在那里做出反应。(成为第一响应者返回 FALSE)
调用 becomeFirstResponder 的正确位置在哪里?
谢谢
编辑:实际上问题是因为我在 cellForRowAtIndexPath 中有以下行:
cell.textField.enabled = NO;
除非表格处于编辑模式,否则我不希望 textFields 处于活动状态,因此我禁用它们并且仅在表格处于编辑模式时在 didSelectCellAtIndexPath 中重新启用它们。
为什么您应该发布所有代码的主要示例,而不仅仅是您认为相关的代码片段。