简体   繁体   English

如何在iPhone中的分组表视图单元格中创建文本字段

[英]How to create a Text Field in Grouped Table view Cell in iPhone

I want to create a text field in the group table view cell. 我想在组表视图单元格中创建一个文本字段。 If I create one text field in a cell, the text fields are over lapped in all the grouped table view cells. 如果我在单元格中创建一个文本字段,则在所有分组的表格视图单元格中重叠文本字段。 I don't know how it happened. 我不知道是怎么回事。 So I want to create a text field in a grouped table view cell (Not all the cells). 所以我想在分组表视图单元格中创建一个文本字段(不是所有单元格)。 How can I achieve this? 我怎样才能做到这一点? Is there any sample code available? 有可用的示例代码吗?

Here my sample code, 这是我的示例代码,

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

just in case anyone comes across this question, this is how you add UITextFields to a group table. 如果有人遇到这个问题,这就是你如何将UITextFields添加到组表中。

In the UITableViewController (which I assume is the table's delegate & datasource) implement the following functions: 在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;
        }
    }           
}

Obviously, you'll still need to implement the UITextFieldDelegate methods to actually process the text entry and moving the first responder between fields. 显然,您仍然需要实现UITextFieldDelegate方法来实际处理文本条目并在字段之间移动第一个响应者。

UPDATE: Since posting this, I realised that you can use the UIControl contentVerticalAlignment property, and set that to UIControlContentVerticalAlignmentCenter, in which case you can just set your frame to the bounds of the cell's content view. 更新:自发布以来,我意识到您可以使用UIControl contentVerticalAlignment属性,并将其设置为UIControlContentVerticalAlignmentCenter,在这种情况下,您可以将帧设置为单元格内容视图的边界。

For good example you can see iPhoneCoreDataRecipes sample. 例如,您可以看到iPhoneCoreDataRecipes示例。 In this sample EditingTableViewCell loaded from nib file, but you can easy construct it in code. 在此示例中,从nib文件加载EditingTableViewCell,但您可以在代码中轻松构造它。

I think that the Daniel solution is not good in some cases. 我认为Daniel解决方案在某些情况下并不好。 For example, if tableview section 0 contains two or more rows, dequeueReusableCellWithIdentifier could return a cell with text field tag wrong. 例如,如果tableview部分0包含两行或更多行,则dequeueReusableCellWithIdentifier可能会返回文本字段标记错误的单元格。 In particular, if a cell was put in the queue when cell at indexPath row 0 was created and dequeueReusableCellWithIdentifier returns this cell when cell at indexPath row 3 was created, the tag of text field will be wrong. 特别是,如果在创建indexPath第0行的单元格时将单元格放入队列,并且dequeueReusableCellWithIdentifier在创建indexPath第3行的单元格时返回此单元格,则文本字段的标记将是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM