Reputation: 259
I've in my tableview i've
self.navigationItem.rightBarButtonItem = self.editButtonItem;
clicking on the minus button shows Delete button. I want to know how to give an action to this Delete button. Tableview shows a list of items from sqlite database.I'm using Xcode 4.2
Upvotes: 1
Views: 1282
Reputation: 530
I would add that you should make sure you are reloading the table data so it can update on the screen. Otherwise it will look like it didn't do anything.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.myMutableArray removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
}
Upvotes: 0
Reputation: 126137
Implement tableView:commitEditingStyle:forRowAtIndexPath:
in your table view data source (presumably your table view controller). See the docs on that method, or any of several Apple sample code projects (including what you get from the Xcode "master-detail app" template) for details & example usage.
Upvotes: 1
Reputation: 5782
When you press delete
button it will automatically call this delegate
method of UITableView
:
- (void)tableView:(UITableView *)tableView commitEditingStyle:( UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath.
{
// Check if it's a delete button or edit button...
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Update the array and table view.
[eventsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
// Commit the change.
NSError *error;
if (![managedObjectContext save:&error])
{
// Handle the error.
}
}
}
Upvotes: 1
Reputation: 12036
If you look in to the UITableViewDataSource Protocol Reference you will find in there tableView:commitEditingStyle:forRowAtIndexPath:
.
Asks the data source to commit the insertion or deletion of a specified row in the receiver.
In this method you will get the index path for the cell to delete. You have to remove the corespondent element in four data source. And you also need to remove the table view cell.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
Upvotes: 1
Reputation: 119242
The delete button is already linked to your table view, and tapping it will send tableView:commitEditingStyle:forRowAtIndexPath:
to your table view's data source. You do your deletion here.
This method is normally included in the template for UITableViewController subclasses.
Upvotes: 4