开发者

Unable to Save Edits to Core Data Attributes

开发者 https://www.devze.com 2023-01-23 04:24 出处:网络
I\'m trying to build a UI so the user can edit the attributes of a core data entity. When the user taps the edit button, selecting a row will push the listDetailViewController, which is just a table v

I'm trying to build a UI so the user can edit the attributes of a core data entity. When the user taps the edit button, selecting a row will push the listDetailViewController, which is just a table view that displays the attributes. It uses a custom table view cell with a label and a UITextField. The listDetailViewController displays the attributes properly, and will accept text as its supposed to, but I can't figure out how to get the user-inputted text to save.

If I'm not explaining clearly, here's an example. I want to change the list's name, so I tap Edit, tap the list, tap the List Name row, the keyboard pops up, I type in the new name, tap Done and it pops me back to the RVC with none of the changes saved. I've been banging my head on this for a few days and would love some help!

Here's the relevant code from 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];
开发者_C百科}

The ivars label and textField are declared in ListDetailCell, which is the table cell nib I mentioned earlier.


Not sure if I've got your problem correct or if my answer is the best method, but it's what I did recently. I have a TableViewController and UITableView which display a series of custom cells for editing data. This is basically replicating what I've seen a number of other applications do to create data editing screens.

Each of the custom cells has a UITextField. When the user finishes editing a cell, the UITextField triggers a message to a UITextFieldDelegate. So I added the UITextFieldDelegate protocol to the TableViewController and when setting up the custom cell in cellForRowAtIndexPath, I set the TableViewController as the UITextFields delegate. Then when the user finishes editing the end editing message is sent and I can then get the value from the UITextField and store it back in the managed entity object.

I apologise, but I don't have access to my code right now or I'd cut and paste an example for you.

Anyway, some things to watch out for:

  • In the code for the delegate message you need to first identify the UITextField that has triggered the call. The best way to do this is to set the Tag property on the UITextField when you create the UITableCell that contains it. Then in the delegate method you can use a switch statement to select which entity field to store the value in.

  • Getting ride of the keyboard when a user taps on a non-editable area of a UITableView can be tricky. You need to store a list of objects that can have a keyboard, and when a click happens, loop through them and do the resign first responder to remove the keyboard from the display.

  • tapping a save button on the navigation bar or something else outside of the UITableView will not remove the keyboard or resign the first responder, so the delegate of the field currently being edited does not get called. You need to add code to trigger the save sequence.

  • If you have any UITextView's they use a different delegate.

To add the delegate you will need to do something like this (taken from your code above):

@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;
        ....
    }
}

This is for dealing with a number of text fields. Another solution I found was to store the changed values in a dictionary and only update the entity when the user taps save. With my solution above, you would also have to reset the entities properties if the user cancels. So it's horse for courses.

0

精彩评论

暂无评论...
验证码 换一张
取 消