保存在tableView中插入新行后成为第一响应者的UITextField文本

问题描述:

我已经实现了在表处于编辑模式时在tableView中插入新行的方法。 当我按下带有绿色“加号”图标的单元格时,一个新的单元格会以绿色的“加号”添加到单元格上方。新单元格包含一个空的textField,它将成为第一响应者并打开键盘。这是我的代码:保存在tableView中插入新行后成为第一响应者的UITextField文本

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

问题:如何保存我在cellArray中的cell.textField中输入的文本?该单元成为第一响应者,从那里我需要一些建议或指导如何将该文本保存在数组中。

您已经设置的UITextField的代表在细胞中,因此只需要实现:

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

我不知道如果小区有一个按钮来保存文本或类似的东西。上述方法会在uitextfield失去焦点时触发。