Reputation: 119
I have an app that takes in 3 user data sections: details for an event, location for an event, and date/time for an event. Each of these is then stored in a cell in the master view controller. I want to be able to delete (using an edit button) each of these cells when needed.
For example, if one of the events becomes irrelevant, I want the user to be able to user the edit button to delete the cells. I have already included an edit button in my storyboard as shown below:
I have never learned how to truly implement an edit button, and cannot find any Apple documentation on how to actually use the edit button. Is there code to implement into my view controller that will accomplish this? Or is there a way to achieve deleting the cell in the storyboard?
EDIT: Here is my project file for reference - File link removed
Upvotes: 1
Views: 1212
Reputation: 6657
The app currently deletes the rows fine, and it appears to work until the next time the app loads. Your mistake is in FinalMasterViewController.m, method tableView: commitEditingStyle: forRowAtIndexPath
has this code:
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedObjectContext deleteObject:[_fetchedResultsController objectAtIndexPath:indexPath]];
}
You are forgetting to SAVE
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedObjectContext deleteObject:[_fetchedResultsController objectAtIndexPath:indexPath]];
//THIS IS THE LINE
[self.managedObjectContext save:nil];
//Without it, the context will not save.
}
Upvotes: 2
Reputation: 6657
In your viewWillAppear:
method, set
[tableview setEditing: NO animated: YES];
Then in the method called by the button, toggle
-(void) editClicked{
if(tableview.editing){
[tableview setEditing: NO animated: YES];
else{
[tableview setEditing: YES animated: YES];
}
}
Upvotes: 1