使用 UITextField 作为类的 .h(我称之为 textField)中的属性创建一个自定义单元格。(我称它为 TextFieldCell)
然后在 didSelectRowAtIndexPath 中有下面的代码。当一个单元格被点击时,您将获得对 TextFieldCells 的引用,然后您可以从中查找 textField 属性并在其上调用 becomeFirstResponder。注意我已经包含了你应该在这个例子中使用的枚举。如果您不知道这些是什么,请将它们放在您的#includes 下方并用谷歌搜索。喜欢枚举!!
//table view sections
enum
{
TableViewSectionUsername = 0,
TableViewSectionPassword,
TableViewSectionLogin,
TableViewSectionCount
};
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TextFieldCell *usernameCell = (TextFieldCell*)[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:TableViewSectionUsername]];
TextFieldCell *passwordCell = (TextFieldCell*)[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:TableViewSectionPassword]];
//switch section
switch(indexPath.section)
{
case TableViewSectionUsername:
{
[[usernameCell textField] becomeFirstResponder];
break;
}
case TableViewSectionPassword:
{
[[passwordCell textField] becomeFirstResponder];
break;
}
case TableViewSectionLogin:
{
if([[[usernameCell textField] text] isEqualToString:@""])
{
NSLog(@"Please enter a username");
[[usernameCell textField] becomeFirstResponder];
return;
}
if([[[passwordCell textField] text] isEqualToString:@""])
{
NSLog(@"Please enter a username");
[[passwordCell textField] becomeFirstResponder];
return;
}
[self dismissViewControllerAnimated:YES completion:nil];
break;
}
default:
{
break;
}
}
//deselect table cell
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
}