开发者

Add UITableViewCell with UITextField Dynamically

开发者 https://www.devze.com 2023-03-26 00:46 出处:网络
I\'m attempting to have a UITableView that I can dynamically add and remove rows from, and the rows have a UITextField in them. For adding the rows, I\'m using the code:

I'm attempting to have a UITableView that I can dynamically add and remove rows from, and the rows have a UITextField in them. For adding the rows, I'm using the code:

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

And I'm just doing a count of nameArray to get how many rows I have in my tableView. Then, inside of cellForRowAtIndexPath, I've got the following code

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

I've got multiple issues with this code. The first issue is I'm doing a preset number of UITextFields, so that I can call them all in textFieldShouldReturn. Is there a good way for me to generate UITextFields that will return when I press the done key?

The second biggest issue with the way I'm doing this right now is my UITextFields get cleared every time I add a new one. Any idea why?


To solve your first issue I would begin by pulling the UITextField creation code into a method..

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

Then invoke the new method in your tableView:cellForRowAtIndexPath: method...

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

Now to make sure that your UITextField's respond to the return key implement the textFieldShouldReturn: method of your UITextFieldDelegate (probably your UITableViewController) to always return true...

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

As for your second issue, I believe this is a result of directly invoking reloadData. This will force your UITableView to recreate its cells. This in turn recreates your UITextFields and you subsequently lose their state/text. I think your next logical step will be to introduce a model (NSMutableArray) that stores the state of each UITextField. You could begin by saving the text of the field into the array upon the UITextFieldDelegate receiving the textFieldShouldReturn message.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号