【发布时间】: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