In this article, we will see how we add pull to refresh to 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 second one of the tableview series and the other is: 
Ingredients
- Visual Studio 2012 or higher / Xamarin Studio 5
- Xamarin iOS 6.4.5 or higher
- Mac OS X Yosemite(10.10) & above.
Procedure
- Create a new Application in Xamarin Studio by selecting New Solution from the the welcome screen.
 
 ![]() 
 
 
- 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.
 
 ![]() 
 
 
- Now, we will name the app and select the minimum iOS version that our app will support, as shown:
 
 ![]() 
 
 
- 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:
 
 ![]() 
 
 
- Now, we can see that the solution file is created and it contains all the required files, as shown below:
 
 ![]() 
 
 
- Now, we will open ViewController.cs from the solution and import CoreGraphics, System, System.Collections.Generic, System.Threading.Tasks and UIKit. - using System;  
- using CoreGraphics;  
- using System.Collections.Generic;  
- using UIKit;  
- using System.Threading.Tasks;  
 
- Create an object of UITableView, TableSource, UIRefreshControl and List<TableItem> in the ViewController class. - UITableView table;  
- TableSource tableSource;  
- bool useRefreshControl = false;  
- UIRefreshControl RefreshControl;  
- List<TableItem> tableItems;  
 
- To set the title of the app in the ViewDidLoad function, just write,- Title = "Pull to Refresh Sample";  
 
- Now, we will add the TableView properties in the ViewDidLoad function. Also, add the TableView to the View and the RefreshControl to the TableView. Finally, our ViewDidLoad() will look as given below:- public override async void ViewDidLoad()  
- {  
-     base.ViewDidLoad();  
-       
-   
-     Title = "Pull to Refresh Sample";  
-     table = new UITableView(new CGRect(0, 20, View.Bounds.Width, View.Bounds.Height - 20));  
-       
-     tableItems = new List<TableItem>();  
-     tableItems.Add(new TableItem("Vegetables") { ImageName = "Vegetables.jpg" });  
-     tableItems.Add(new TableItem("Fruits") { ImageName = "Fruits.jpg" });  
-     tableItems.Add(new TableItem("Flower Buds") { ImageName = "Flower Buds.jpg" });  
-     tableItems.Add(new TableItem("Legumes") { ImageName = "Legumes.jpg" });  
-     tableItems.Add(new TableItem("Tubers") { ImageName = "Tubers.jpg" });  
-     tableSource = new TableSource(tableItems);  
-     table.Source = tableSource;  
-   
-     await RefreshAsync();  
-   
-     AddRefreshControl();  
-   
-     Add(table);  
-     table.Add(RefreshControl);  
- }  
 
- We also add the RefreshAsync() and AddRefreshControl() methods for the UIRefreshControl to perform the PullToRefresh action. - async Task RefreshAsync()  
- {  
-       
-     if (useRefreshControl)  
-         RefreshControl.BeginRefreshing();  
-   
-     if (useRefreshControl)  
-         RefreshControl.EndRefreshing();  
-   
-         table.ReloadData();  
- }  
-  
- #region * iOS Specific Code  
-   
-   
- void AddRefreshControl()  
- {  
-     if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0))  
-     {  
-           
-         RefreshControl = new UIRefreshControl();  
-         RefreshControl.ValueChanged += async (sender, e) =>  
-         {  
-             tableItems.Add(new TableItem("Bulbs") { ImageName = "Bulbs.jpg" });  
-             await RefreshAsync();  
-         };  
-         useRefreshControl = true;  
-     }  
- }  
- #endregion  
 
- Now, we will add a new class, TableSource, to the solution.
 
 ![]() 
 
 
- Once the file is added, open TableSource.cs and add the code, given below:- using System;  
- using System.Collections.Generic;  
- using System.IO;  
- using Foundation;  
- using UIKit;  
-   
- namespace PullToRefresh  
- {  
-     public class TableSource : UITableViewSource  
-     {  
-         List<TableItem> tableItems;  
-         protected string cellIdentifier = "TableCell";  
-   
-         public TableSource(List<TableItem> items)  
-         {  
-             tableItems = items;  
-         }  
-   
-         public override nint RowsInSection(UITableView tableview, nint section)  
-         {  
-             return tableItems.Count;  
-         }  
-   
-         public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)  
-         {  
-               
-             UITableViewCell cell = tableView.DequeueReusableCell(cellIdentifier);  
-   
-   
-             var cellStyle = UITableViewCellStyle.Default;  
-   
-               
-             if (cell == null)  
-             {  
-                 cell = new UITableViewCell(cellStyle, cellIdentifier);  
-             }  
-   
-             cell.TextLabel.Text = tableItems[indexPath.Row].Title;  
-             cell.ImageView.Image = UIImage.FromFile("Images/" + tableItems[indexPath.Row].ImageName);  
-   
-             return cell;  
-         }  
-   
-         public override nint NumberOfSections(UITableView tableView)  
-         {  
-             return 1;  
-         }  
-  
-  
-         #region -= editing methods =-  
-   
-         public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)  
-         {  
-             switch (editingStyle)  
-             {  
-                 case UITableViewCellEditingStyle.Insert:  
-                     tableItems.Insert(indexPath.Row, new TableItem("(inserted)"));  
-                     tableView.InsertRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Fade);  
-                     break;  
-   
-                 case UITableViewCellEditingStyle.None:  
-                     Console.WriteLine("CommitEditingStyle:None called");  
-                     break;  
-             }  
-         }  
-   
-         public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath)  
-         {  
-             return true;   
-         }  
-   
-         public override UITableViewCellEditingStyle EditingStyleForRow(UITableView tableView, NSIndexPath indexPath)  
-         {  
-             if (tableView.Editing)  
-             {  
-                 if (indexPath.Row == tableView.NumberOfRowsInSection(0) - 1)  
-                     return UITableViewCellEditingStyle.Insert;  
-                 else  
-                     return UITableViewCellEditingStyle.Delete;  
-             }  
-             else    
-                 return UITableViewCellEditingStyle.Delete;  
-         }  
-   
-         public void WillBeginTableEditing(UITableView tableView)  
-         {  
-             tableView.BeginUpdates();  
-   
-             tableView.InsertRows(new NSIndexPath[] {  
-                     NSIndexPath.FromRowSection (tableView.NumberOfRowsInSection (0), 0)  
-                 }, UITableViewRowAnimation.Fade);  
-             tableItems.Add(new TableItem("(add new)"));  
-   
-             tableView.EndUpdates();  
-         }  
-  
-         #endregion  
-     }  
- }  
 
 
 
- Now, we will add another class, TableItem, to the solution for the customized cell style. Once the file is added, open TableItem.cs and add the code, given below:- using System;  
- using UIKit;  
-   
- namespace PullToRefresh  
- {  
-     public class TableItem  
-     {  
-         public string Title { get; set; }  
-   
-         public string ImageName { get; set; }  
-   
-         public UITableViewCellStyle CellStyle  
-         {  
-             get { return cellStyle; }  
-             set { cellStyle = value; }  
-         }  
-         protected UITableViewCellStyle cellStyle = UITableViewCellStyle.Default;  
-   
-         public UITableViewCellAccessory CellAccessory  
-         {  
-             get { return cellAccessory; }  
-             set { cellAccessory = value; }  
-         }  
-         protected UITableViewCellAccessory cellAccessory = UITableViewCellAccessory.None;  
-   
-         public TableItem() { }  
-   
-         public TableItem(string title)  
-         { Title = title; }  
-   
-     }  
- }  
 
- Now,  we run the app o the iPhone simulator of our choice (I did it on iPhone 6S) and it looks, as shown below:
 
 ![]() 
 
 
- Once we pull to refresh the app, it looks:
 
 ![]() 
 
 
- To see if it has refreshed or not, we have added an item in the AddRefreshControl method. Thus, 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 myGit.