How handle UITableView Edit option using Swipe in iOS apps?

Hi Friends,

Today am going to post to handle UITableView Editing mode with Swipe control.

We have to add a bar button item named as “Edit” in left hand side of UINavigationBar. When the user clicks on the button we have to change the “Edit” button to “Done” and add the edit mode to the tableview.

UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editingTable)];
self.navigationItem.leftBarButtonItem = editButton;

Add the method for UIBarButtonItem action,

-(void)editingTable
{
// Here we are using a single button to two purposes edit and done…

if (TableView.editing)
{
isTableEditMode = NO;

[TableView setEditing:NO animated:YES];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editingTable)];
self.navigationItem.leftBarButtonItem = editButton;
[TableView reloadData];
}
else
{
if ([NameArray count] >0)
{
isTableEditMode = YES;

[TableView setEditing:YES animated:YES];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editingTable)];
self.navigationItem.leftBarButtonItem = doneButton;
[TableView reloadData];
}
}
}

Add the delegates which are going to help us to edit the tableview.

-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

– (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Detemine if it’s in editing mode
if (TableView.editing)
{
return UITableViewCellEditingStyleDelete;
}
else
{
self.navigationItem.rightBarButtonItem = nil;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editingTable)];
self.navigationItem.leftBarButtonItem = doneButton;
isTableEditMode = YES;
tableViewIsInEdit = YES;
return UITableViewCellEditingStyleDelete;
}
}

If condition will be called when the user select the “Edit” button from the navigation bar. Else condition will be called when the user swipe the tableview to delete the items from the tableview.

editingStyleForRowAtIndexPath: is the delegate decide the editing mode for example the tableview should me delete or editing.

When the user selects the Delete button from the editing mode the below delegate will be called. Here we can find the indexpath and we can delete the data from NSMutableArray, NSArray and Core Data.

-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSString *selectedName = [NameArray objectAtIndex:indexPath.row];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@”customerName == %@”,selectedName];
NSEntityDescription *entity = [NSEntityDescription entityForName:@”Messages” inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];

NSError *error;
NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];

for (NSManagedObject *managedObject in items)
{
[managedObjectContext deleteObject:managedObject];
}
if (![managedObjectContext save:&error])
{
NSLog(@”Error  :%@”,[error description]);
}
}
}

I have deleted the selected indexpath item from CoreData. You can modify the able code in the If condition block from NSMutableArray or etc.,

If the user delete the data from Swipe control we have to use below code to terminate the Swipe editing mode.

-(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
isTableEditMode = NO;

[TableView setEditing:NO animated:YES];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editingTable)];
self.navigationItem.leftBarButtonItem = editButton;
[TableView reloadData];

tableViewIsInEdit = NO;
}

I hope this will help you little bit and save some of your valuable time. Happy coding.

Thanks.

Warm Regards,

Yuvaraj.M