Swipe Table Cell To Perform RowAction In TableView In Xamarin.iOS

Introduction

In this article, we will learn how to create an iOS application using Xamarin. We will learn how to add and work with a table view and its row swipe action. We will have a table of contents and we will swipe the row to the left to perform an action. 

Prerequisite

  1. Basic knowledge of C# and programming.
  2. Knowledge of Xamarin.

This article will cover the following.

  1. Xamarin iOS application.
  2. Table View in the application.
  3. TableViewDelegate for performing TableViewAction.

Implementation

Open Visual Studio Community Edition.

Xamarin  

Select the single view app and click "Next".

Xamarin

Give the app a name and click "Next".

Xamarin  

Give project and solution names and create the application.

Open Main.storyboard and add the Table View to your controller.

Open toolbox pad and search for tableview and drag the View to your controller.

Xamarin  

Resize it and set the name property to “CarsTable”.

Add two class files to your solution.

  1. CarSource.cs
    1. using System;  
    2. using System.Collections.Generic;  
    3. using Foundation;  
    4. using UIKit;  
    5.   
    6. namespace SwipeTable {  
    7.     public class CarSource : UITableViewSource {  
    8.         List<String> cars;  
    9.         public CarSource(List<String> source) {  
    10.             this.cars = source;  
    11.         }  
    12.   
    13.         public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {  
    14.             var cell = new UITableViewCell(UITableViewCellStyle.Default, "");  
    15.             cell.TextLabel.Text = cars[indexPath.Row];  
    16.             return cell;  
    17.         }  
    18.   
    19.         public override nint RowsInSection(UITableView tableview, nint section) {  
    20.             return cars.Count;  
    21.         }  
    22.     }  
    23. }  
  1. CarDelegate.cs
    1. using System;  
    2. using UIKit;  
    3.   
    4. namespace SwipeTable {  
    5.     public class CarsDelegate : UITableViewDelegate {  
    6.         public override UITableViewRowAction[] EditActionsForRow(UITableView tableView, Foundation.NSIndexPath indexer) {  
    7.             var action = UITableViewRowAction.Create(  
    8.                 UITableViewRowActionStyle.Default, "Like", (arg1, arg2) => {  
    9.                     var cell = tableView.CellAt(arg2);  
    10.                     cell.TextLabel.Text += " ( Liked )";  
    11.                 }  
    12.             );  
    13.             return new UITableViewRowAction[] { action };  
    14.         }  
    15.     }  
    16. }  

Open ViewController.cs and add the below contents.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Foundation;  
  4. using UIKit;  
  5.   
  6. namespace SwipeTable  
  7. {  
  8.     public partial class ViewController : UIViewController  
  9.     {  
  10.         protected ViewController(IntPtr handle) : base(handle)  
  11.         {  
  12.             // Note: this .ctor should not contain any initialization logic.  
  13.         }  
  14.   
  15.         public override void ViewDidLoad()  
  16.         {  
  17.             base.ViewDidLoad();  
  18.             var cars = new List<String>  
  19.             {  
  20.                 "Mercedes""BMW""Porsche""Ferrari""Lamborghini""Mazerati""Jaguar""Lexus""Toyota","Ford""Audi"  
  21.             };  
  22.             CarsTable.Source = new CarSource(cars);  
  23.             CarsTable.Delegate = new CarsDelegate();  
  24.         }  
  25.   
  26.         public override void DidReceiveMemoryWarning()  
  27.         {  
  28.             base.DidReceiveMemoryWarning();  
  29.             // Release any cached data, images, etc that aren't in use.  
  30.         }  
  31.     }  

Your solution directory structure will be as shown below.

Xamarin  

Build and run the application.

You will have the table view with the given strings of contents as cars.

Xamarin  

Now, swipe left any cell content (suppose we swipe the element BMW).

Xamarin  

Now, click on Like button.

Xamarin  

You can see the element has been liked.

Now, click anywhere on the screen and you can see the Table View with its data.

Xamarin

Note

We are creating the table source in class file  CarSource.cs and assigning it to the added table view in our viewController named CarsTable.

Then for adding the functionalities like the ability to manage selections, configure section headers and footers, delete and reorder cells of the table source you require UITableViewDeimglelgate to provide behavior along with the data that is getting from UITableViewSource.

Implementing UITableView often requires both the subclasses. So, in this way, you can achieve the Table View with data with attached behavior as called swipe of the cell in this demo which is possible by overriding the method EditActionsForRow and creating the UITableViewRowAction.Create an event that is the static method under the class UITableViewRowAction.


Similar Articles