【问题标题】:How to create a Text Field in Grouped Table view Cell in iPhone如何在 iPhone 的分组表格视图单元格中创建文本字段
【发布时间】:2017-05-03 18:26:03
【问题描述】:

我想在组表视图单元格中创建一个文本字段。如果我在单元格中创建一个文本字段,则文本字段在所有分组的表格视图单元格中重叠。我不知道它是怎么发生的。所以我想在分组的表格视图单元格(不是所有的单元格)中创建一个文本字段。我怎样才能做到这一点?有没有可用的示例代码?

这是我的示例代码,

if(indexPath.section == 0)
{
    if(indexPath.row == 0)
    {
        UITextField * lastname = [[UITextField alloc] initWithFrame:CGRectMake(120, 5, 150, 35)];

        lastname.tag = titleTag2;

        [cell.contentView addSubview:lastname];

        lastname.delegate = self;

        lastname.returnKeyType = UIReturnKeyDefault;
    }
}

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    以防万一有人遇到这个问题,这就是您将 UITextFields 添加到组表的方式。

    在 UITableViewController(我假设它是表的委托和数据源)中实现以下功能:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"CellIdentifier";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
            if (indexPath.section == 0) {
    
                // Add a UITextField
                UITextField *textField = [[UITextField alloc] init];
                // Set a unique tag on each text field
                textField.tag = titleTag2 + indexPath.row;
                // Add general UITextAttributes if necessary
                textField.enablesReturnKeyAutomatically = YES;
                textField.autocorrectionType = UITextAutocorrectionTypeNo;
                textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
                [cell.contentView addSubview:textField];
                [textField release];
            }
        }
    
        // Configure the cell...
        [self configureCell:cell atIndexPath:indexPath];
    
        return cell;
    }
    
    
    - (void)configureCell:(UITableViewCell *)theCell atIndexPath:(NSIndexPath *)indexPath {
    
        if (indexPath.section == 0) {
    
            // Get the text field using the tag
            UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:titleTag2+indexPath.row];
            // Position the text field within the cell bounds
            CGRect cellBounds = theCell.bounds;
            CGFloat textFieldBorder = 10.f;
            // Don't align the field exactly in the vertical middle, as the text
            // is not actually in the middle of the field.
            CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );
    
            textField.frame = aRect;
    
            // Configure UITextAttributes for each field
            if(indexPath.row == 0) {
                textField.placeholder = @"Name";
                textField.returnKeyType = UIReturnKeyNext;
                textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
            } else if(indexPath.row == 1) {
                textField.placeholder = @"Email Address";
                textField.returnKeyType = UIReturnKeyNext;
                textField.keyboardType = UIKeyboardTypeEmailAddress;
            } else {
                textField.placeholder = @"Password";
                textField.returnKeyType = UIReturnKeyDone;
                textField.secureTextEntry = YES;
            }
        }           
    }
    

    显然,您仍然需要实现 UITextFieldDelegate 方法来实际处理文本输入并在字段之间移动第一响应者。

    更新:自从发布此内容后,我意识到您可以使用 UIControl contentVerticalAlignment 属性,并将其设置为 UIControlContentVerticalAlignmentCenter,在这种情况下,您只需将框架设置为单元格内容视图的边界即可。

    【讨论】:

      【解决方案2】:

      您可以查看iPhoneCoreDataRecipes 示例。 在这个从 nib 文件加载的示例 EditingTableViewCell 中,您可以轻松地在代码中构建它。

      【讨论】:

        【解决方案3】:

        我认为 Daniel 的解决方案在某些情况下并不好。例如,如果 tableview 部分 0 包含两行或更多行,则 dequeueReusableCellWithIdentifier 可能会返回文本字段标记错误的单元格。特别是,如果在创建indexPath第0行的单元格时将一个单元格放入队列,而当indexPath第3行的单元格创建时dequeueReusableCellWithIdentifier返回此单元格,则文本字段的标签将是错误的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-18
          相关资源
          最近更新 更多