【问题标题】:ios - adding text fields to uitableviewcell programmatically not workingios - 以编程方式将文本字段添加到 uitableviewcell 不起作用
【发布时间】:2013-07-15 13:20:44
【问题描述】:

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

这是我的单元格的渲染方式。我想我在重复使用细胞的部分做了一些愚蠢的事情。我确实在stackoverflow上检查了一些其他类似的线程,但没有一个有帮助。你能否告诉我我错过了什么,或者这甚至是应该这样做的方式。非常感谢您的意见。

谢谢, 迈克

- (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.contentView 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.contentView 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.contentView 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.contentView addSubview:self.password];
}

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

[self.signUpTable addSubview:self.firstName];
[self.signUpTable addSubview:self.lastName];
[self.signUpTable addSubview:self.emailId];
[self.signUpTable addSubview:self.password];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

}

【问题讨论】:

标签: ios objective-c uitableview uitextfield


【解决方案1】:

试试这个..

     - (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;
    }

不需要在表格视图中再次添加文本视图,这是错误的。试试这个,这对你有用,但更好的是你应该创建一个自定义 Cell 并使用。这是最好的方法,因为您可以随心所欲地使用 Cell。

【讨论】:

    【解决方案2】:

    不要将它们添加为子视图,将它们设置为单元格的附属视图。

    - (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 和内存使用,并不是说这是最不优雅的解决方案。

    【讨论】:

      【解决方案3】:

      看起来您只有四个要显示的单元格。这是一个静态案例,单元格不会改变,您应该在 xib/storyboard 中静态创建这个 tableView 及其单元格。这是这里的首选方式。

      如果您真的想以编程方式执行此操作,请事先创建四个具有您想要的行为的 UITableViewCell。将它们保存在一个数组中。 cellForRowAtIndex 内部路径return cellArray[indexPath.row];

      请注意,这是最好的方法,因为您只有四个 tableViewCell。 仅当您的单元格数量超过屏幕上一次可以容纳的单元格时,ReuseIdentifiers 才会派上用场。因此,在您的情况下,您实际上从未重复使用单元格。

      【讨论】:

        【解决方案4】:

        保留这个并尝试一下,

        - (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.
            }
        }
        

        【讨论】:

          【解决方案5】:

          尝试使用这个......并且没有必要将这些文本字段添加到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;
          
          }
          

          【讨论】:

            【解决方案6】:

            无需编写更多单元格,只需创建一个带有文本字段的自定义单元格。为每个文本字段指定标签,并在 numberOfRowsIntheSections 中设置行数,然后它将显示您需要多少单元格。我想它会对你有所帮助。

            【讨论】:

              【解决方案7】:

              在您按照我的回答之前,我想告诉您以下代码不利于内存管理,因为它会为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;
              }
              

              【讨论】:

                【解决方案8】:

                @Mike :首先,我建议您使用 StoryBoard 或 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;
                }
                

                您可以分配不同的标签,并根据标签存储这些值。

                【讨论】:

                  猜你喜欢
                  • 2023-03-17
                  • 2016-02-02
                  • 2011-11-08
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-03-03
                  • 1970-01-01
                  相关资源
                  最近更新 更多