【问题标题】:Add UITableViewCell with UITextField Dynamically动态添加带有 UITextField 的 UITableViewCell
【发布时间】:2011-10-21 04:42:08
【问题描述】:

我正在尝试拥有一个 UITableView,我可以从中动态添加和删除行,并且这些行中有一个 UITextField。为了添加行,我使用的是代码:

- (void) addRow
    {
        [nameArray addObject:[NSString stringWithFormat:@"%i", x]];
        [self.tableView reloadData];
        x++;
    }

我只是在计算 nameArray 以获取我的 tableView 中有多少行。然后,在 cellForRowAtIndexPath 内部,我得到了以下代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    /*Default Apple-y stuff*/
    ...
    if ([indexPath row] == 0) {
        playerTextFieldZero = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 185, 30)];
        playerTextFieldZero.adjustsFontSizeToFitWidth = YES;
        playerTextFieldZero.placeholder = @"Name";
        playerTextFieldZero.textColor = [UIColor blackColor];
        playerTextFieldZero.returnKeyType = UIReturnKeyDone;
        playerTextFieldZero.backgroundColor = [UIColor whiteColor];
        playerTextFieldZero.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        playerTextFieldZero.textAlignment = UITextAlignmentLeft;
        playerTextFieldZero.tag = 0;
        playerTextFieldZero.delegate = self;
        playerTextFieldZero.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
        [playerTextFieldZero setEnabled: YES];
        [cell addSubview:playerTextFieldZero];
        [playerTextFieldZero becomeFirstResponder];
        [playerTextFieldZero release];
    }
    ...
    /*More of those in here*/
    ...
return cell;
}

此代码存在多个问题。第一个问题是我正在做一个预设数量的 UITextField,这样我就可以在 textFieldShouldReturn 中调用它们。有没有一种好方法可以生成当我按下完成键时返回的 UITextFields?

我现在这样做的第二大问题是我的 UITextFields 每次添加新的时都会被清除。知道为什么吗?

【问题讨论】:

    标签: iphone xcode uitableview uitextfield


    【解决方案1】:

    为了解决您的第一个问题,我将从将 UITextField 创建代码拉入一个方法开始。

    - (UITextField*)textFieldForCell:(UITableViewCell*)cell withDelegate:(id<UITextFieldDelegate>*)delegate {
        UITextField textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 185, 30)];
        textField.delegate = self;
        ....
        [cell addSubview:playerTextFieldZero];
        [textField release];
    }
    

    然后在你的 tableView:cellForRowAtIndexPath: 方法中调用新方法...

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ...
        // Custom initialization code    
        [self textFieldForCell:cell withDelegate:self];
    }
    

    现在要确保您的 UITextField 对返回键的响应实现 UITextFieldDelegate(可能是您的 UITableViewController)的 textFieldShouldReturn: 方法以始终返回 true...

    -(bool)textFieldShouldReturn:(UITextField*)textField {
        return YES;
    }
    

    至于你的第二个问题,我相信这是直接调用reloadData的结果。这将强制您的 UITableView 重新创建其单元格。这反过来会重新创建您的 UITextFields,您随后会丢失它们的状态/文本。我认为您的下一个合乎逻辑的步骤将是引入一个模型(NSMutableArray)来存储每个 UITextField 的状态。您可以首先在 UITextFieldDelegate 接收到 textFieldShouldReturn 消息时将字段的文本保存到数组中。

    【讨论】:

    • 太棒了!这很好地回答了我的问题。
    猜你喜欢
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多