Objective-c – How to delete a row of a Table in iPhone

iphoneobjective cuitableview

I have a UITableView and I want to provide the functionality to user to delete the row when he slips or flicks his finger on the row. I know the editing style which provides a circular red button with -ve sign on it. But How to implement the flicking style. I saw many applications using it, so does apple provide any inbuilt delegate for it or we need to write our own controller for it.

Best Answer

In order to get the swipe affect you need to implement the table view delegate

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

method, this will provide access to the swipe interaction for deletion. I typically provide an edit interaction as well for tableviews where deletion is possible since the swipe interaction tends to be a little bit hidden from users.

As an example:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView beginUpdates];    
  if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Do whatever data deletion you need to do...
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];   
  }       
  [tableView endUpdates];
}

Hope that helps.