Title: how delete a data in core data with tableview
Tags: ios;objective-c;uitableview;core-data
Question: I can not find how to delete a core data entity instance from my table view.

This is how I update the table view with information from core data:

```- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    Datos * record = [self.fetchedRecordsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ ",record.punto];

    UIImage* image = [UIImage imageWithData:record.imagen];
    cell.imageView.image = image;

    return cell; 
}
```

and I am trying to use the following function to delete the entity instance, but it isn't working:

```-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{  
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
```

Comment: Why not to use NSFetchedResultsController??


Here is another answer: You need to delete the entity instance from the managed object context:

```NSManagedObjectContext *moc = record.managedObjectContext;
Datos * record = [self.fetchedRecordsArray objectAtIndex:indexPath.row];
[self.fetchedRecordsArray removeObject:record];
[moc deleteObject:record];

NSError *error;

if (![moc save:&amp;error]) {
    NSLog(@"Save error181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16 %@", error);
}
```

this goes in ```tableView:commitEditingStyle:forRowAtIndexPath:```

(Assuming that ```Datos``` is your managed object subclass)

Comment for this answer: Forgot the save. Debug, is the object deleted? What is the save error?
Comment for this answer: The error is that whatever you're using to populate the table (provide the row count) isn't having an item removed. My answer is now removing an item from your array and Core Data. That should cover the bases. What do you use the return the row count?
Comment for this answer: ok, yes it is, but the error is the index of the row of tableview i update my question
Comment for this answer: the error is not the elimination of the data in the core data, the error is not cleared in the tableview, I'm using [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationAutomatic], but does not work
Comment for this answer: return [self.fetchedRecordsArray count];

Here is another answer: Well, i'm not sure where you get your data from your CoreData but you need to call a that kind of snippet, it won't delete by itself when deleting row

```- (void) deleteElement:(NSString*)elementId{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"YourTable" inManagedObjectContext:self.managedObjectContext]];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"id=%@",elementId]];

    NSError *error=nil;
    NSManagedObject *fetchedElement = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&amp;error] lastObject];

    [self.managedObjectContext deleteObject:fetchedElement];

    if (![self.managedObjectContext save:&amp;error]) {
        NSLog(@"problem deleting item in coredata : %@",[error localizedDescription]);
    }
}
```

And your code should look like :

```-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

     [self deleteElement:idOfElementToDelete];
}
```

this is the error:

'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Comment for this answer: I'm sorry, you're right the error is not the elimination of the data in the core data, the error is not cleared in the tableview, I'm using [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationAutomatic], but does not work
Comment for this answer: You shouldn't add another problem in an answer, it won't help somebody with the same problem, and could be confusing. It is better to add a comment. 
Your new error has nothing to do with CoreData, it says that if you delete a row, the new total should match the number given in 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Comment for this answer: You can check that link http://stackoverflow.com/questions/5043729/core-data-example-invalid-update-invalid-number-of-rows-in-section-0
It is about inserting a new row but it is the same issue.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:03/12/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><objective-c><uitableview><core-data>', 'Title': 'how delete a data in core data with tableview', 'ViewCount': 1198, 'id': 22355495, 'user_AboutMe': '', 'user_AccountId': 4071307.0, 'user_DisplayName': 'lycros', 'user_Reputation': 193.0, 'user_Views': 28.0, 'user_id': 3344287.0}}