我正在尝试构建用户界面,以便用户可以编辑核心数据实体的属性。当用户点击编辑按钮时,选择一行将推动listDetailViewController
,这只是一个显示属性的表格视图。它使用带有标签和UITextField的自定义表格视图单元格。 listDetailViewController
正确显示属性,并会接受文本,但我不知道如何让用户输入的文本保存。无法将编辑保存到核心数据属性
如果我没有解释清楚,这里是一个例子。我想更改列表的名称,所以我点击编辑,点击列表,点击列表名称行,弹出键盘,输入新名称,点击完成,然后弹出我回到RVC,没有任何更改保存。我一直在这个问题上敲打我的头几天,并会喜欢一些帮助!
下面是相关的代码从ListDetailViewController
:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(done)];
self.navigationItem.rightBarButtonItem = doneButton;
[doneButton release];
self.tableView.allowsSelection = NO;
self.tableView.allowsSelectionDuringEditing = NO;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 3;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *DetailCellIdentifier = @"DetailCell";
ListDetailCell *cell = (ListDetailCell *)[tableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"ListDetailCell" owner:self options:nil];
cell = listDetailCell;
self.listDetailCell = nil;
// Configure the cell...
// list name
if (0 == indexPath.row) {
cell.label.text = @"List Name";
cell.textField.text = self.selectedList.listName;
cell.textField.placeholder = @"Name";
}
// Detail 1
if (1 == indexPath.row) {
cell.label.text = @"Detail 1";
cell.textField.text = selectedList.detail1;
cell.textField.placeholder = @"Detail 1";
}
// Detail 2
if (2 == indexPath.row) {
cell.label.text = @"Detail 2";
cell.textField.text = selectedList.detail2;
cell.textField.placeholder = @"Detail 2";
}
}
return cell;
}
- (void)done {
[self.listDetailCell resignFirstResponder];
[self.navigationController popViewControllerAnimated:YES];
}
label
和textField
在ListDetailCell
声明,这是表格单元格笔尖我前面提到的高德。
我应该将委托添加到单元类,还是添加到'cellForRowAtIndexPath'方法?键盘不是问题,因为完成按钮和导航控制器按照他们应有的工作。为了清楚起见,我编辑了我的示例以更清晰! – 2010-11-04 01:14:34
我上面更新了一些示例代码。小心输入错误,我把它输入:-) – drekka 2010-11-04 02:38:34
这样做,谢谢!我仍然围绕着代表围绕我的大脑,并且每一个新点都有所帮助。 :) – 2010-11-04 04:06:19