【问题标题】:Save UITextField text that become first responder after inserting new row in tableView在 tableView 中插入新行后保存成为第一响应者的 UITextField 文本
【发布时间】:2014-04-10 20:57:10
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios uitableview uitextfield uitextfielddelegate


    【解决方案1】:

    您已经在单元格中设置了 UITextField 的委托,所以只需实现:

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

    我不确定单元格是否有一个按钮来保存文本或类似的东西。上面的方法会在 uitextfield 失去焦点时触发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 2010-12-16
      • 1970-01-01
      • 2011-08-21
      • 2013-05-25
      • 1970-01-01
      相关资源
      最近更新 更多