Show native style delete button on swiping a row in UITableView

UITableView is a common control used in each and every app that display a list whether its a mail list or another app functionality. In this post we will add delete button on tableview row when user swipes same as it happened in native apple apps so that user can delete an item from that list by swiping on that particular item.First reaction from or a newbie like me, who are implementing this feature for the first time is “This cannot be possible as its provided by Apple to their own apps”.
 
                                                                       But you can use this feature in your app as well. In this article we will cover this topic which lets you to use same functionality of Delete button appearing on right side when user swipe on an item in a list or in technical term when user swipe on a row in UITableView.

To use this feature we need to implement a delegate method of UITableView named as

– (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
As soon as you implement or write this delegate method and with empty code in its definition, like
– (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

}
you will see when you swipe on row of your UITableView a red button with Delete tittle starts appearing on your row. If you tap on it nothing will happen as the above method fired up and since there is nothing written inside this method.
 We will write  code/logic for deleting our row or object from our array as UITableView display our array data as its rows. We will use deleteRowsAtIndexPath method to remove/ delete row from UITAbleView.This will remove single row and not reload whole UITableView. You have to remove object from your array too, if you did not remove object from your array then Xcode will throw exception. 
– (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [alertArray removeObjectAtIndex:indexPath.row]; 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
To change tittle of your delete button you can use below given delegate
– (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @”BORRAR”;
}