0

I'm working on an application that uses core data, and I wanted to add a gesture if the user swipes to the left it will delete the object in the database. I have the gesture recognizer parts all set up, I'm having trouble creating the method to remove the object from the tableview and core data. So if anyone could look at this and point me into the right direction I would appreciate it! Thanks!

TableView.M

-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
        NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
        NSManagedObjectContext *context = [self managedObjectContext];

            // Delete object from database
            [context deleteObject:[self.lists objectAtIndex:indexPath.row]];

            // Remove list from table view
            [self.lists removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        }

@end

Error:

self(ViewController *)  0x8ea0a90   
indexPath=(NSIndexPath *    )  nil  
context+(NSManagedObjectContext *)  0x8d747f0   
Jack
  • 304
  • 6
  • 19
  • What is the problem with what you have? Perhaps you just forgot to call `[context save:nil]` afterwards to save your changes? – Dima Apr 10 '14 at 03:29
  • Ok I added that line of code and also added: `[self.tableView reloadData];` Then it started to crash here:`[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];` When I commented that out it worked I just need some sort on animation of the cell fading when deleting. – Jack Apr 10 '14 at 03:41
  • Crashes how? Saying what? What exception is raised? – Tommy Apr 11 '14 at 03:46
  • Are you using a NSFetchedResultsController? If you are, then that's where the deleteRows should happen. If not, you might consider it; it takes care of a lot of problems. – mackworth Apr 11 '14 at 04:07
  • It's deleting the objects when I remove that line, but when I have it it crashes. I updated the question with the error. – Jack Apr 11 '14 at 21:30

1 Answers1

0

Try this

-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
    NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
    NSManagedObjectContext *context = [self managedObjectContext];

        // Delete object from database
        [context deleteObject:[self.lists objectAtIndex:indexPath.row]];
        [context save:nil];

        // Remove list from table view
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.lists removeObjectAtIndex:indexPath.row];
}
Keshav
  • 2,965
  • 3
  • 25
  • 30
  • `[self.tableView deleteRowsAtIndexPaths:[self.lists objectAtIndex:indexPath] withRowAnimation:UITableViewRowAnimationFade];` This is the line causing it to crash when I remove it I'm fine I just no longer have a sliding/deleting animation. – Jack Apr 10 '14 at 03:50
  • Received another crash. – Jack Apr 10 '14 at 03:58
  • I'm still having this issue. – Jack Apr 10 '14 at 23:18
  • Can you give me the crash log? – Keshav Apr 11 '14 at 03:31
  • check these links https://discussions.apple.com/message/7461909#7461909 http://stackoverflow.com/questions/4186251/crash-on-deleterowsatindexpaths – Keshav Apr 11 '14 at 04:34