我想在 UITableView 中编辑用户的用户名。我向 UITableViewCell 添加了一个 UITextField ,这似乎工作得很好。但是当用户触摸单元格(甚至在文本字段之外)时,他希望进行编辑。
如何以编程方式选择文本字段?
代码看起来像这样:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
userName.selected = TRUE;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cellSelected = [tableView cellForRowAtIndexPath: indexPath];
UITextField *textField = [[cellSelected.contentView subviews] objectAtIndex: 0];
[textField becomeFirstResponder];
[tableView deselectRowAtIndexPath: indexPath animated: NO];
}
这假设您没有更快的方法来知道哪个 UITextField 对象位于哪个单元格中,并且您确定知道 UITextField 将是第一个子视图。
您可能还想检查 [textField isFirstResponder] ——如果它已经是firstResponder,则没有必要将其设置为firstResponder。但这可能不是必要的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)