Note that there are some explanatory texts on larger screens.

plurals
  1. PODeleting Core Data from a UITableView within a UIViewController
    primarykey
    data
    text
    <p>I have a UITableView within a UIViewController so that I can have a UILabel below the table. In doing so, I have had difficulties in adding an Edit/Done button. I couldn't do the traditional way, so I had to do a work around using the following idea:</p> <p>1)Create the edit button up the top left on viewdidload:</p> <pre><code>self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editbutton)]; </code></pre> <p>2)Create code so that upon clicking the edit button, the tableview becomes editable, and it changes the title to done. Then upon clicking done, it goes back to saying edit.</p> <pre><code>-(IBAction)editbutton{ self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donebutton)]; [tableView setEditing:YES animated:YES]; } -(IBAction)donebutton{ self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editbutton)]; [tableView setEditing:NO animated:YES]; } </code></pre> <p>This part is all OK. I just have put it in for completeness. The tableview becomes editable when click the edit button, and i can click done and it goes back to normal. My problem is clicking the delete button (after clicking the Red minus button next to a row) doesn't delete the row. I have tried the following code:</p> <p>NOTE: 1)context has been declared in the .h file as: @property (nonatomic, retain) NSManagedObjectContext *context; and synthesized in the .m file. 2)I have declared the @property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController in the .h file and then the @synthesize fetchedResultsController = _fetchedResultsController in the .m file</p> <pre><code>- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the managed object for the given index path [context deleteObject:[_fetchedResultsController objectAtIndexPath:indexPath]]; // Save the context. NSError *error = nil; if (![context save:&amp;error]) { /* Replace this implementation with code to handle the error appropriately. abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. */ NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } } </code></pre> <p><strong>EDIT:</strong></p> <p>Ok, I have found a bit of a solution. I used the following code to delete my core data:</p> <pre><code>- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the managed object for the given index path NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; // Save the context. NSError *error = nil; if (![context save:&amp;error]) { /* Replace this implementation with code to handle the error appropriately. abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. */ NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } [self fetchedresults]; [self.tableView reloadData]; } } </code></pre> <p>My new problem is that when i click delete. The row is deleted but still remains there with the red minus button on a blank row. I can still click on the row too (which normally edits the data) but there is no data to load.</p> <p>EDIT 2:</p> <p>I forgot to add, to get it to work, i added this: </p> <pre><code>- (NSFetchedResultsController *)fetchedResultsController{ if (_fetchedResultsController != nil) { return _fetchedResultsController; } // Set up the fetched results controller. // Create the fetch request for the entity. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; // Edit the entity name as appropriate. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Record" inManagedObjectContext:context]; [fetchRequest setEntity:entity]; // Set the batch size to a suitable number. [fetchRequest setFetchBatchSize:20]; // Edit the sort key as appropriate. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"activity" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; // Edit the section name key path and cache name if appropriate. // nil for section name key path means "no sections". NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:@"Master"]; aFetchedResultsController.delegate = self; self.fetchedResultsController = aFetchedResultsController; NSError *error = nil; if (![self.fetchedResultsController performFetch:&amp;error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return _fetchedResultsController; } </code></pre> <p>EDIT 3:</p> <p>This is what fetched results does:</p> <pre><code>- (void)fetchedresults { NSManagedObjectContext *moc = [self context]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Record" inManagedObjectContext:moc]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entityDescription]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"activity" ascending:YES]; [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; NSError *error = nil; NSArray *array = [moc executeFetchRequest:request error:&amp;error]; if (array == nil) { // Deal with error... } float tot = [[array valueForKeyPath:@"@sum.cpdhours"] floatValue]; totalhours.text = [NSString stringWithFormat:@"%.1f", tot]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload