无法将编辑保存到核心数据属性

时间:2010-11-03 23:33:06

标签: iphone core-data

我正在尝试构建UI,以便用户可以编辑核心数据实体的属性。当用户点击编辑按钮时,选择一行将推送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];
}

ivars labeltextFieldListDetailCell中声明,这是我之前提到的表格单元格。

1 个答案:

答案 0 :(得分:3)

不确定我的问题是否正确,或者我的答案是最好的方法,但这是我最近所做的。我有一个TableViewController和UITableView,它显示一系列用于编辑数据的自定义单元格。这基本上复制了我所看到的许多其他应用程序创建数据编辑屏幕的内容。

每个自定义单元格都有一个UITextField。当用户完成对单元格的编辑时,UITextField会触发到UITextFieldDelegate的消息。所以我将UITextFieldDelegate协议添加到TableViewController中,当在cellForRowAtIndexPath中设置自定义单元格时,我将TableViewController设置为UITextFields委托。然后,当用户完成编辑时,发送结束编辑消息,然后我可以从UITextField获取值并将其存储回管理实体对象。

我很抱歉,但我现在无法访问我的代码,或者我为您剪切并粘贴了一个示例。

无论如何,有些事情需要注意:

  • 在委托消息的代码中,您需要首先识别已触发呼叫的UITextField。执行此操作的最佳方法是在创建包含UITextField的UITableCell时在UITextField上设置Tag属性。然后在委托方法中,您可以使用switch语句选择要存储值的实体字段。

  • 当用户点击UITableView的不可编辑区域时,驾驭键盘可能会非常棘手。您需要存储可以有键盘的对象列表,当发生单击时,循环遍历它们并重新签名第一响应者以从显示中删除键盘。

  • 点击导航栏上的保存按钮或UITableView外部的其他内容不会删除键盘或辞退第一响应者,因此不会调用当前正在编辑的字段的代理。您需要添加代码来触发保存序列。

  • 如果你有任何UITextView,他们会使用不同的代表。

要添加委托,您需要执行以下操作(取自上面的代码):

@interface MyTableViewController : UITableViewController <UITextFieldDelegate>

....

    if (0 == indexPath.row) {
        cell.label.text = @"List Name";
        cell.textField.text = self.selectedList.listName;
        cell.textField.placeholder = @"Name";

        cell.textField.delegate = self; // Setting controller as text field delegate.
        cell.textField.tag = 1; // Really should use an enum here for clarity.

    }

....

-(void) textFieldDidEndEditing:(UITextField *) textField {
    switch(textField.tag) {
        case 1: //Again with the enum.
            // Save field 1.
            entity.someProperty = textField.text;
        ....
    }
}

这是为了处理许多文本字段。我发现的另一个解决方案是将更改的值存储在字典中,并仅在用户点击保存时更新实体。使用上面的解决方案,如果用户取消,您还必须重置实体属性。所以这是课程的马。

相关问题