I'm working on an app using CoreData that lets user add or delete items to a TableView. Before deleting an item, I'd like to give them the option to verify they really want to delete with an AlertView.
I have the AlertView setup here:
Code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSString *message = [[NSString alloc] initWithFormat:@"Do you really want to delete ?"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Alert"
message: message
delegate: self
cancelButtonTitle: @"Cancel"
otherButtonTitles: @"OK", nil];
[alert show];
[alert release];
[message release];
That's working fine and the buttons are set up properly.
I've put the call to delete the object here:
Code:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSIndexPath *indexPath = [myTableView indexPathForSelectedRow];
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error;
if (![context save:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
else {
}
}
I thought it was working fine because it deleted the item in the first row.
The problem is is that it always deletes the top row, no matter what row I've selected. i.e., if I've added 3 items (3 rows) to the table view, when I try to delete the 3rd row and hit the delete button, the top row disappears. When I hit delete again, the 2nd row (now the top row) disappears.
If someone could point me in the right direction on using an Alert View with CoreData I'd appreciate it. I couldn't find anything to help at the Developer web site.