Simple Table View in Xamarin iOS

In this article, we will learn to create a simple table view in Xamarin iOS. Table View is a view for displaying the list of data in the scroll function.

Introduction

 
Hello guys, in this article, we will learn to create a simple table view in Xamarin iOS. Table View is a view for displaying the list of data in the scroll function. But, Xamarin iOS doesn’t have an option to directly display the data, so we need to create a custom table view Source class and configure row cell view or layout.
 

iOS output

 
 

Prerequisites

  • Visual Studio 2017 or above
  • XCode 10.0 or above
  • IOS Device or Simulator
  • This project created in Visual Studio 2019 with Mac Machine

Let’s Start

 
Step 1
 
Open Visual Studio and create Xamarin iOS Single View Application by going to Visual Studio >> New Solution >> In the dialog left side, select iOS under App >> forward by select Single View Application and click Next.
 
 
Step 2:
 
In the next dialog window, give your app name, organization name, and target version, choose devices then click Next.
 
 
After a few seconds, the project is created and you get the solution folder structure like below.
 
 
Step 3:
 
Now, open Main.storyboard by going to Solution Pad >> Main.StoryBoard >> right-click to select Choose iOS Designer or XCode Interface Builder >> Afterwards, the storyboard will be opened. Here, drag and drop UITableView and set Constraints. Top, Bottom, Leading and Trailing Constraints are Zero.
 
 
Setting up constraints, you get a view like below.
 
 
Now, you can set the UITableView identifier as tableviewId and View Controller as ViewController.Cs. We should set Source and all other properties for this layouttableviewId. Otherwise, we can’t have access to this Table View.
 
Step 4:
 
Next, create a new class named ListTableViewSource.cs. By going to the right-click Solution and Add >> New File >> Class and give name ListTableViewSource.cs. This class inherits from UITableViewSource and extracts the Abstract Class.  The page code is given below.
    Step 5:
    1. class TableViewSimpleSource: UITableViewSource {  
    2.  private List < string > listItems = new List < string > () {  
    3.   "List Item",  
    4.   "List Item",  
    5.   "List Item",  
    6.   "List Item",  
    7.   "List Item",  
    8.   "List Item",  
    9.   "List Item",  
    10.   "List Item",  
    11.   "List Item",  
    12.   "List Item",  
    13.   "List Item",  
    14.   "List Item",  
    15.   "List Item",  
    16.   "List Item",  
    17.   "List Item",  
    18.   "List Item"  
    19.  };  
    20.  public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {  
    21.   var cell = new UITableViewCell(CGRect.Empty);  
    22.   cell.TextLabel.Text = listItems[indexPath.Row];  
    23.   return cell;  
    24.  }  
    25.  public override nint RowsInSection(UITableView tableview, nint section) {  
    26.   return listItems.Count;  
    27.  }  
    Now, open ViewController.cs file and set the Table View Source Class to Table View. The code is given below.
      Step 6:
      1. using System;  
      2. using System.Collections.Generic;  
      3. using CoreGraphics;  
      4. using Foundation;  
      5. using UIKit;  
      6. namespace TableViewDemo {  
      7.  public partial class ViewController: UIViewController {  
      8.   public ViewController(IntPtr handle): base(handle) {}  
      9.   public override void ViewDidLoad() {  
      10.    base.ViewDidLoad();  
      11.    tableviewId.Source = new TableViewSimpleSource();  
      12.   }  
      13.   public override void DidReceiveMemoryWarning() {  
      14.    base.DidReceiveMemoryWarning();  
      15.   }  
      16.  }  
      Now, run your Xamarin iOS application and we get output like below.
       
       
       
      Get Source From Github
       
      Feedback: Thank you for reading this, if you have any suggestions, feel free to write in the comments section.