Custom Table View In Xamarin iOS

Custom Table View In Xamarin iOS
 

Introduction

 
In my previous article, we discussed the TableView and their default cell behaviors. Now, we going to learn about how to use a custom table view cell in TableView.
 
Let's start.
 
Create Xamarin iOS Single View Application by going to Visual Studio >> New >> Project >> Single view Application under iOS section.
 
After the project creation open Main.StoryBoard and place UITableView. Set the TableView frame size equal to view size and set constraints.
 
Custom Table View In Xamarin iOS 
 
Now, create new Table View Cell by  right-clicking the project and select Add >> followed by selecting new File. In the dialog window's left plane select iOS and in center plane select Table View Cell template and give the file a name then click create [Eg- MyTableViewCell].
 
Custom Table View In Xamarin iOS
 
Afterward, the new XIB and cs extension file will be created with your given name. Now, open the XIB file in your designer (my favorite XCode). Next,  drag a button from the toolbox and place at the beginning of the cell. Also,  place another label after the button.
 
Note
You must give Cell reuse identifier, otherwise you won't have access for the cell.
 
Custom Table View In Xamarin iOS
 
Next, open MyTableViewCell.cs file and create a new static method named UpdateData. This method is used to update the contents of the cell because we couldn't access the cell subviews from another class.
  1. using System;  
  2. using Foundation;  
  3. using UIKit;  
  4.   
  5. namespace CustomTableViewCell  
  6. {  
  7.     public partial class MyCardViewCell : UITableViewCell  
  8.     {  
  9.         public static readonly NSString Key = new NSString("MyCardViewCell");  
  10.         public static readonly UINib Nib;  
  11.   
  12.         static MyCardViewCell()  
  13.         {  
  14.             Nib = UINib.FromName("MyCardViewCell", NSBundle.MainBundle);  
  15.         }  
  16.   
  17.         protected MyCardViewCell(IntPtr handle) : base(handle)  
  18.         {  
  19.             // Note: this .ctor should not contain any initialization logic.  
  20.         }  
  21.         public void UpdateData(string text)  
  22.         {  
  23.             title.Text = text;  
  24.         }  
  25.     }  
  26. }  
Now, create a new Table View Source Class named MyTableViewSource.cs. This class should be inherited from UITableViewSource and override GetCell, RowSelect and RowInSelection method.
  1. using System;  
  2. using Foundation;  
  3. using UIKit;  
  4.   
  5. namespace CustomTableViewCell  
  6. {  
  7.     public class MyTableViewSource : UITableViewSource  
  8.     {  
  9.         private string[] titles;  
  10.         private INavigator navigator;  
  11.   
  12.         public MyTableViewSource(INavigator navigator, params string[] titles)  
  13.         {  
  14.             this.titles = titles;  
  15.             this.navigator = navigator;  
  16.         }  
  17.   
  18.         public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)  
  19.         {  
  20.             var cell = tableView.DequeueReusableCell("MyCardViewCell_ID", indexPath) as MyCardViewCell;  
  21.             cell.UpdateData(titles[indexPath.Row]);  
  22.   
  23.             return cell;  
  24.   
  25.         }  
  26.   
  27.         public override nint RowsInSection(UITableView tableview, nint section)  
  28.         {  
  29.             return titles.Length;  
  30.         }  
  31.         public override void RowSelected(UITableView tableView, NSIndexPath indexPath)  
  32.         {  
  33.             base.RowSelected(tableView, indexPath);  
  34.             navigator.Navigate(indexPath.Row);  
  35.         }  
  36.     }  
  37. }  
Finally, open ViewController class and apply source class to the table view source. Here, you should call ReloadData static method. Create an array of string and pass-through constructor and apply the values to the cell. 
  1. using System;  
  2. using UIKit;  
  3.   
  4. namespace CustomTableViewCell  
  5. {  
  6.     public partial class ViewController : UIViewController, INavigator  
  7.     {  
  8.         public ViewController(IntPtr handle) : base(handle)  
  9.         {  
  10.         }  
  11.   
  12.         public override void ViewDidLoad()  
  13.         {  
  14.             base.ViewDidLoad();  
  15.             // Perform any additional setup after loading the view, typically from a nib.  
  16.   
  17.             tableView.RegisterNibForCellReuse(MyCardViewCell.Nib, "MyCardViewCell_ID");  
  18.             tableView.Source = new MyTableViewSource(this"Remainder""Remainder 1""Remainder 22""Remainder 3""Remainder 55""Remainder 66");  
  19.   
  20.             tableView.RowHeight = UITableView.AutomaticDimension;  
  21.             tableView.ReloadData();  
  22.         }  
  23.   
  24.         public override void DidReceiveMemoryWarning()  
  25.         {  
  26.             base.DidReceiveMemoryWarning();  
  27.             // Release any cached data, images, etc that aren't in use.  
  28.         }  
  29.   
  30.         public void Navigate(int index)  
  31.         {  
  32.             // Do Navigation code   
  33.         }  
  34.     }  
  35. }  
Here, we have one problem, if the user selects the row, the event will trigger in the source class, but I need to navigate to another ViewController based on selection, there is no option to navigate view controllers. So, I need to create an interface and pass it to the source class. Now user selects row, and the view controller method will be invoked using Interface. Write your navigate code logics here.
  1. using System;  
  2. namespace CustomTableViewCell  
  3. {  
  4.     public interface INavigator  
  5.     {  
  6.         void Navigate(int index);  
  7.     }  
  8. }   
It's time to run our application and our application output looks like below.
 
Custom Table View In Xamarin iOS
 

Summary 

  • Create a new Table View
  • Add new Table View Cell and design the cell
  • Create a TableView source class
  • Apply source class to TableView
  • Create a new Navigate interface and use it.
Thanks for reading!!!


Similar Articles