Delete A Row In TableView By Swiping And Handling Row Selection


In this article, we are going to learn how to delete a row in TableView by Swiping and how to handle the item click event of TableView.


In my last tutorial we saw how to create a simple table view. Just refer to our previous tutorial because we are going to continue from there.

Here is link of previous tutorial

Here are the steps required to delete a row in TableView by Swipe:

Step 1

First we need to override the CanEditRow method


If CommitEditingStyle is overridden, all rows are assumed to be editable. If this method is implemented and returns false (for some specific rows, or for all rows) then the swipe-to-delete gesture will not be available in that cell.

  1. public override bool CanEditRow(UITableView tableView, Foundation.NSIndexPath indexPath) {  
  2.     return true;  
  3. }  

Step 2

Handle the click of delete button. For this we need to integrate CommitEditingStyle method.


The table source detects if this method is overridden and automatically enables the swipe-to-delete gesture. The method’s implementation should call DeleteRows on the UITableView to cause the cells to disappear, and also remove the underlying data from your model (for example, an array, dictionary or database).

  1. public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, Foundation.NSIndexPath indexPath) {  
  2.     switch (editingStyle) {  
  3.         case UITableViewCellEditingStyle.Delete:  
  4.             tabledata.RemoveAt(indexPath.Row);  
  5.             tableView.DeleteRows(new NSIndexPath[] {  
  6.                 indexPath  
  7.             }, UITableViewRowAnimation.Fade);  
  8.             break;  
  9.         case UITableViewCellEditingStyle.None:  
  10.             break;  
  11.     }  
  12. }  

Step 3

By default delete Button text is “Delete”. If you want to change the name of delete button. First override the method TitleForDeleteConfirmation as mention below.(Otional)

  1. public override string TitleForDeleteConfirmation(UITableView tableView, Foundation.NSIndexPath indexPath)   
  2. {  
  3.     return "Trash";  
  4. }  

Handle Row Selection

To handle user row selection we have to override RowSelection method UITableViewSource, using NSIndexPath indexpath to determine which row was selected.

  1. public override void RowSelected(UITableView tableView, Foundation.NSIndexPath indexPath)  
  2. {  
  3.     new UIAlertView("Row Selected", tabledata[indexPath.Row], null"OK"null).Show();  
  4.     tableView.DeselectRow(indexPath, true);  
  5. }  

Now the code looks like this,

  1. public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath) {  
  2.     return true// return false if you wish to disable editing for a specific indexPath or for all rows  
  3. }  
  4. public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, Foundation.NSIndexPath indexPath) {  
  5.     switch (editingStyle) {  
  6.         case UITableViewCellEditingStyle.Delete:  
  7.             // remove the item from the underlying data source  
  8.             tabledata.RemoveAt(indexPath.Row);  
  9.             tableView.DeleteRows(new NSIndexPath[] {  
  10.                 indexPath  
  11.             }, UITableViewRowAnimation.Fade);  
  12.             break;  
  13.         case UITableViewCellEditingStyle.None:  
  14.             break;  
  15.     }  
  16. }  
  17. public override string TitleForDeleteConfirmation(UITableView tableView, Foundation.NSIndexPath indexPath) {  
  18.     return "Trash";  
  19. }  
  20. public override void RowSelected(UITableView tableView, Foundation.NSIndexPath indexPath) {  
  21.     new UIAlertView("Row Selected", tabledata[indexPath.Row], null"OK"null).Show();  
  22.     tableView.DeselectRow(indexPath, true); // iOS convention is to remove the highlight  
  23. }  


Try to build and run the project, if everything is ok as I mention then your app will look like this,

  1. After swipe delete button will appear

  2. After click on delete button row data deleted,

  3. After clicking on row, alert will appear


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now