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添加文本字段无效_Ios_Objective C_Uitableview_Uitextfield - Fatal编程技术网

ios-以编程方式向uitableviewcell添加文本字段无效

ios-以编程方式向uitableviewcell添加文本字段无效,ios,objective-c,uitableview,uitextfield,Ios,Objective C,Uitableview,Uitextfield,新手问题。我正在尝试使用表视图控制器创建一个注册表单。我试图在CellForRowatineXpath方法中以编程方式向每个单元格添加文本字段,但我的所有文本字段似乎都是在第一个单元格上创建的,一个单元格与另一个单元格重叠。这是我的密码 下面是我的单元格的渲染方式。我想我在重复使用电池的部分做了些傻事。我确实检查了stackoverflow上的一些其他类似线程,但没有一个有帮助。你能告诉我我错过了什么吗?或者这就是我应该做的事情。非常感谢你的意见 谢谢, 迈克 不要将它们添加为子视图,将它们设

新手问题。我正在尝试使用表视图控制器创建一个注册表单。我试图在CellForRowatineXpath方法中以编程方式向每个单元格添加文本字段,但我的所有文本字段似乎都是在第一个单元格上创建的,一个单元格与另一个单元格重叠。这是我的密码

下面是我的单元格的渲染方式。我想我在重复使用电池的部分做了些傻事。我确实检查了stackoverflow上的一些其他类似线程,但没有一个有帮助。你能告诉我我错过了什么吗?或者这就是我应该做的事情。非常感谢你的意见

谢谢, 迈克


不要将它们添加为子视图,将它们设置为单元的辅助视图

- (UITextField*)getTextField{
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 20, 35)];
    tf.delegate = self;
    tf.textColor        = [UIColor colorWithRed:.231 green:.337 blue:.533 alpha:1];
    tf.autocorrectionType = UITextAutocorrectionTypeNo;
    tf.borderStyle = UITextBorderStyleNone;
    tf.frame = CGRectMake(0, 20, 170, 30);
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    tf.font = [UIFont systemFontOfSize:13];
    return tf;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        cell.detailTextLabel.numberOfLines = 2;
    }

    if (indexPath.section == 0) {

        UITextField *tf = (UITextField*)cell.accessoryView;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.numberOfLines = 2;
        tf                  = [self getTextField];
        cell.accessoryView = cell.editingAccessoryView = tf;
        [((UITextField*)cell.accessoryView) setBorderStyle:self.tableView.editing ? UITextBorderStyleRoundedRect : UITextBorderStyleNone];
        [((UITextField*)cell.accessoryView) setUserInteractionEnabled:self.tableView.editing ? YES : NO];
        [((UITextField*)cell.accessoryView) setTextAlignment:!self.tableView.editing ? UITextAlignmentRight : UITextAlignmentLeft];
        ((UITextField*)cell.accessoryView).tag = indexPath.row;
    }

    return cell;
}

其思想是,单元格每次出现在屏幕上时都会从屏幕外重新绘制,如果您使用
addSubview:
添加文本字段,它也会每次添加它。您可以这样做,但是您必须清除单元格的
contentView
子视图,但这需要额外的工作、cpu和内存使用,更不用说这是最不优雅的解决方案了。

保留此解决方案并尝试它

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *MyIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.row,indexPath.section];


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
     //your stuff.
    }
}

尝试使用这个…并且没有必要将这些文本字段添加到tableview。所以请删除一些代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"signUpFields" forIndexPath:indexPath];

// Configure the cell...
if (indexPath.row == 0){
    //self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
    self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.firstName.placeholder = @"First Name";
    self.firstName.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.firstName setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.firstName;
    [cell addSubview:self.firstName];
}

if (indexPath.row == 1){
    self.lastName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.lastName.placeholder = @"Last Name";
    self.lastName.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.lastName setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.lastName;
    [cell addSubview:self.lastName];
}

if (indexPath.row == 2){
    self.emailId = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.emailId.placeholder = @"Email Id";
    self.emailId.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.emailId setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.emailId;
    [cell addSubview:self.emailId];
}

if (indexPath.row == 3){
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.password.placeholder = @"Password";
    self.password.secureTextEntry = YES;
    self.password.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.password setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.password;
    [cell addSubview:self.password];
}

self.firstName.delegate = self;
self.lastName.delegate = self;
self.emailId.delegate = self;
self.password.delegate = self;

cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

}

只需创建一个带有textfield的自定义单元格,而不必写入更多单元格。为每个textfield指定标记,并在这些节中设置NumberOfRows中的行数,然后它将显示您需要的单元格数。我想这会对您有所帮助。

在您按照我的回答回答之前,我想告诉您,以下代码对内存管理不利,因为它会为
UITableView
的每一行创建新的单元格,所以请小心

但是最好使用,当
UITableView
的行数有限时(大约50-100行),那么下面的代码对您的情况很有帮助,如果适合您,请使用它

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


         /// Put/Create  your UITextField or any Controls here


     }

    return cell;
}
试试这个

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

       if (indexPath.row == 0){
    //self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
    self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.firstName.placeholder = @"First Name";
    self.firstName.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.firstName setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.firstName;
    [cell addSubview:self.firstName];
}

if (indexPath.row == 1){
    self.lastName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.lastName.placeholder = @"Last Name";
    self.lastName.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.lastName setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.lastName;
    [cell addSubview:self.lastName];
}

if (indexPath.row == 2){
    self.emailId = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.emailId.placeholder = @"Email Id";
    self.emailId.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.emailId setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.emailId;
    [cell addSubview:self.emailId];
}

if (indexPath.row == 3){
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.password.placeholder = @"Password";
    self.password.secureTextEntry = YES;
    self.password.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.password setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.password;
    [cell addSubview:self.password];
}

self.firstName.delegate = self;
self.lastName.delegate = self;
self.emailId.delegate = self;
self.password.delegate = self;


cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }

不需要在表视图中再次添加文本视图,这是错误的。试试这个,这会对你有用,但最好你创建一个自定义单元格并使用它。这是最好的方法,因为您可以使用任何您想要的单元格。

看起来您只有四个要显示的单元格。这是一种静态情况,单元格不会更改,您应该在xib/情节提要中静态创建此tableView及其单元格。这是这里的首选方式

如果您确实希望以编程方式执行此操作,请先创建四个具有所需行为的UITableViewCell。让他们排成一列。在cellforrowatinex路径
内返回[indexPath.row]

注意,这是最好的方法,因为您只有四个TableViewCell。
只有当您的单元格数量超过屏幕上一次可容纳的数量时,重用标识符才会派上用场。因此,在你的情况下,你从来没有真正重用过单元格。

@Mike:首先,我建议你使用故事板或Nib文件

如果您使用这个,那么您可以使用以下代码

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

     UITextField *txtField = (UITextField *)[cell viewWithTag:1];
     txtField.text = @"Latest";

     return cell;
}
根据行号可以设置文本

如果要在运行时添加UITextField。然后您可以使用以下代码

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
     UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(165.0, 7.0, 150.0, 30.0)];
     txtField.text = @"Latest";
     [cell.contentView addSubview:txtField];
     return cell;
}

您可以指定不同的标记,并根据标记存储这些值。

为什么不创建自定义UITableViewCell?并实现prepareforeuse功能,重置单元数据。apple doc中的更多信息为什么要将UITextFields添加到tableView?
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
     UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(165.0, 7.0, 150.0, 30.0)];
     txtField.text = @"Latest";
     [cell.contentView addSubview:txtField];
     return cell;
}