Search code examples
iosuitableviewuitextfielduitextfielddelegate

Save UITextField text that become first responder after inserting new row in tableView


I have implemented the methods to insert new row in tableView while the table is in Editing mode. When i press the cell with the green "plus" icon, a new cell is added above the cell with the green "plus". The new cell contains an empty textField that becomes first responder and opens the keyboard. Here is my code:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

    [super setEditing:editing animated:animated];
    if (editing) {
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:[itemsArray count] inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
    } else {
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:[itemsArray count] inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];

    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row >= [itemsArray count]) {
        return UITableViewCellEditingStyleInsert;
    } else {
        return UITableViewCellEditingStyleDelete;
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.tableView beginUpdates];
        [itemsArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];

    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        [self.tableView beginUpdates];
        [itemsArray addObject:@""];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
        ClientCell * cell = (ClientCell*)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
        cell.cellText.enabled = YES;
        cell.cellText.delegate = self;
        [cell.cellText becomeFirstResponder];
    }
}

Question: how to save the text that I type in the cell.textField in itemsArray? The cell becomes first responder and from there I need some suggestions or guidance of how to save that text in the array.


Solution

  • You are already setting the delegate of the UITextField in the cell, so just implement:

    - (void)textFieldDidEndEditing:(UITextField *)textField {
        NSString *text = textField.text;
        // Do whatever you want with the text, like putting it in the array
    }
    

    I'm not sure if the cell has a button to save the text or something similar. The method above will trigger when the uitextfield loses the focus.