Xamarin.iOS: TableView Recipe

In this article, we will see how we use the tableview control. Tableview is a very important and most widely used control. Thus, we will be having a lot of the tutorials that focus on the different aspects of it. This is the first one of the tableview series. We will be describing briefly about the tableview, talk about some common methods, and properties which are very beneficial and mandatory.


  • Visual Studio 2012 or higher / Xamarin Studio 5
  • Xamarin iOS 6.4.5 or higher
  • Mac OS X Yosemite(10.10) & above.


  1. Create a new Application in Xamarin Studio by selecting New Solution from the welcome screen.

  2. Select iOS app from the installed templates. Now, as you can see, we have a lot of options, like Single View app, Master-Detail app, Tabbed app. These are nothing but pre-defined UI screens for the ease-of-use for the developers. For this tutorial, we will select Single View app.

  3. Now, we will name the app and select the minimum iOS version that our app will support, as shown:

  4. Now, we will name the project, give the location for the solution to be saved and select the version control for git (if required), as shown:

  5. Now, we can see that the solution file is created and it contains all the required files, as shown below:

  6. Now, we will open ViewController.cs from the solution and import CoreGraphics and System.Collections.Generic. 
    1. using CoreGraphics;  
    2. using System.Collections.Generic;  
  7. Create an object of UITableView in the ViewController class. 
    1. UITableView table;  
  8. To set the title of the app in the ViewDidLoad function, just write:
    1. Title = "Table View Sample";  
  9. Now, we will add the TableView properties in the ViewDidLoad function. Also, add the TableView to the View. Finally, our ViewDidLoad() will look, as given below:
    1. public override void ViewDidLoad()  
    2.         {  
    3.             base.ViewDidLoad();  
    4.             // Perform any additional setup after loading the view, typically from a nib.  
    5.             Title = "Table View Sample";  
    6.             table = new UITableView(new CGRect(0, 20, View.Bounds.Width, View.Bounds.Height - 20));  
    7.             //table.AutoresizingMask = UIViewAutoresizing.All;  
    8.             string[] tableItems = new string[] { "Apple""Motorola""Microsoft""Google""Nokia" };  
    9.             table.Source = new TableSource(tableItems);  
    12.             Add(table);  
    13.         }  
  10. Now, we will add a new class TableSource to the solution.

  11. Once the file is added, open TableSource.cs and add the code, given below:
    1. using System;    
    2. using System.Collections.Generic;    
    3. using System.IO;    
    4. using Foundation;    
    5. using UIKit;    
    8. namespace TableViewSample    
    9. {    
    10.     public class TableSource : UITableViewSource    
    11.     {    
    12.         protected string[] tableItems;    
    13.         protected string cellIdentifier = "TableCell";    
    15.         public TableSource(string[] items)    
    16.         {    
    17.             tableItems = items;    
    18.         }    
    19.         public override nint RowsInSection(UITableView tableview, nint section)    
    20.         {    
    21.             return tableItems.Length;    
    22.         }    
    24.        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)    
    25.         {    
    26.             // request a recycled cell to save memory    
    27.             UITableViewCell cell = tableView.DequeueReusableCell(cellIdentifier);    
    28.             // if there are no cells to reuse, create a new one    
    29.             if (cell == null)    
    30.                 cell = new UITableViewCell(UITableViewCellStyle.Default, cellIdentifier);    
    32.             cell.TextLabel.Text = tableItems[indexPath.Row];    
    34.             return cell;    
    35.         }    
    38.         public override nint NumberOfSections(UITableView tableView)    
    39.         {    
    40.             return 1;    
    41.         }    
    42.     }    
    43. }  
    This class is used to implement the abstract class UITableViewSource. Thus, we define the basic functions required for a TableView. RowsInSection, which is used to determine the number of rows present in a section, NumberOfSections is used to determine the number of sections the TableView is divided and the GetCell defines the UITableViewCell will be rendered for a particular row.

  12. Now, we run the app on the iPhone simulator of our choice (I did it on iPhone 6S) and it looks, as shown below:
Make some beautiful apps, using Xamarin.iOS and tweet your queries to @adiiaditya. If you want to fork this project, visit my Git.

Recommended Ebook

Xamarin.Forms For Beginners

Download Now!
Similar Articles