Create TableView In Xamarin.iOS

After we complete our sample app, it will look like the following.

Xamarin IOS

Create Sample Project

I am going to create this project in Mac using Visual Studio 2017.

Select Single View application inside the iOS tab.

Xamarin IOS

Give this project a name as "SampleTableView".

Xamarin IOS

Once the new project is created, we have some default files and a storyboard that we are going to use further in our sample application.

Xamarin IOS

Now, open MainStoryboard.storyboard.

  1. Drag and drop navigation Controller from ToolBox in story board and remove the default TableViewController.
  2. Drag and drop ViewController.
  3. Drag and drop TableView inside the View Controller and give it name as "mainTableView”.
  4. Select Navigation Controller and hold down the control key, drag mouse from navigation Controller to View Controller. On moving mouse up, a popup menu will appear letting us choose the action root.
  5. Set Navigation Controller as Initial View Controller.

    Xamarin IOS

For displaying data to the TableView, we need a source class that passes data and information like how many rows and which type of cell we are going to use for TableView.

For that, we are going to use implementation of UITableViewSource and assign it to the UITableView.

There are only two mandatory methods to make a table display data,

  • RowsInSection – return a nint count of the total number of rows of data the table should display
  • GetCell – return a UITableCellView populated with data for the corresponding row index passed to the method.

For adding this Table Source file into your project, go to the solution and select "Add new file" and choose Empty Class in General tab.

Xamarin IOS

Find this TableViewSource file from solution and open it.

We need to implement UITableViewSource file into our TableSource file. For that,

  1. Import UIKit into TableSource file with “using UIKit”
  2. Inherit UITableViewSource to your file using “:” symbol and implement its methods : RowsInSection and GetCell
  3. Declare a List of string named tableData.

    In TableSource method insert a list of string parameter.

    1. public TableViewSource(List < string > items) {  
    2.     tabledata = items;  
    3. }  
  4. Now, in RowInSection -

    1. public override nint RowsInSection(UITableView tableview, nint section) {  
    2.     return tabledata.Count;  
    3. }  
  5. In GetCell method -

    1. public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {  
    2.     UITableViewCell cell = tableView.DequeueReusableCell("cell");  
    3.     if (cell == null) {  
    4.         cell = new UITableViewCell(UITableViewCellStyle.Default, "cell");  
    5.     }  
    6.     cell.TextLabel.Text = tabledata[indexPath.Row];  
    7.     return cell;  
    8. }  

Now, our TableViewSource file looks like this.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Foundation;  
  4. using UIKit;  
  5. namespace XamarinTableView {  
  6.     public class TableViewSource: UITableViewSource {  
  7.         List < string > tabledata;  
  8.         public TableViewSource(List < string > items) {  
  9.             tabledata = items;  
  10.         }  
  11.         public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {  
  12.             UITableViewCell cell = tableView.DequeueReusableCell("cell");  
  13.             if (cell == null) {  
  14.                 cell = new UITableViewCell(UITableViewCellStyle.Default, "cell");  
  15.             }  
  16.             cell.TextLabel.Text = tabledata[indexPath.Row];  
  17.             return cell;  
  18.         }  
  19.         public override nint RowsInSection(UITableView tableview, nint section) {  
  20.             return tabledata.Count;  
  21.         }  
  22.     }  
  23. }  

To use this subclass, first, create a list of strings and assign this list to Table View source. The View Controller ViewDidLoad method looks like this,

  1. public override void ViewDidLoad()  
  2. {  
  3.     base.ViewDidLoad();  
  4.     List < string > itemData = new List < string > ()  
  5.     {  
  6.         "Android",  
  7.         "IOS",  
  8.         "Windows Phone",  
  9.         "Xamarin-IOS",  
  10.         "Xamarin-Form",  
  11.         "Xamarin_Android"  
  12.     };  
  13.     mainListview.Source = new TableViewSource(itemData);  
  14. }  

Now, try to build and run the app. If you did all the things right, it’ll look like this.

Xamarin IOS

As you see in the above output, the blank cell border is shown. For removing this blank border, we need to add one line.

  1. mainListview.TableFooterView = new UIView(CoreGraphics.CGRect.Empty);  

Xamarin IOS

One last step -- cell border is not left-aligned. For this, we need to call "willdisplay" method in TableViewSource file as mentioned below.

  1. public override void WillDisplay(UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)  
  2. {  
  3.     if (cell.RespondsToSelector(new ObjCRuntime.Selector("setSeparatorInset:"))) cell.SeparatorInset = UIEdgeInsets.Zero;  
  4.     if (cell.RespondsToSelector(new ObjCRuntime.Selector("setLayoutMargins:"))) cell.LayoutMargins = UIEdgeInsets.Zero;  
  5. }  

Again, run the application and check the output.

Xamarin IOS


Similar Articles