Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCell,其中包含UITextField_Ios_Xcode_Uitableview - Fatal编程技术网

Ios UITableViewCell,其中包含UITextField

Ios UITableViewCell,其中包含UITextField,ios,xcode,uitableview,Ios,Xcode,Uitableview,我正在创建一个类似于iOS原生联系人应用程序中的“新联系人”的表单 我找到的唯一方法是创建表视图和自定义表视图单元格 到目前为止还不错 现在,我的TextField只在单击时获得焦点,但我想在单击我创建的表视图单元格类的任何位置时将焦点设置为TextField 我试过: - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated];

我正在创建一个类似于iOS原生联系人应用程序中的“新联系人”的表单

我找到的唯一方法是创建表视图和自定义表视图单元格

到目前为止还不错

现在,我的TextField只在单击时获得焦点,但我想在单击我创建的表视图单元格类的任何位置时将焦点设置为TextField

我试过:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    [self.txtInputer becomeFirstResponder];
    // Configure the view for the selected state
}

但是它没有如我所愿工作,焦点被设置到表的最后一个字段。

在类的.h(我称之为textField)中创建一个自定义单元格,并将UITextField作为属性。(我叫它TextFieldCell)

然后在didSelectRowAtIndexPath中输入以下代码。当点击一个单元格时,您会得到一个对TextFieldCells的引用,然后从中可以查找textField属性并调用becomeFirstResponder。注意,我已经包括了本例中应该使用的枚举。如果你不知道这些是什么,把它们放在你的#include下面,用谷歌搜索。我爱你

//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];

}

@user2270849我知道你是个新手,但你需要将你的问题标题命名为更能描述你的问题的名称。