How to populate a table from a controller in Xamarin?

I am currently filling out a table view as follows:

public class DataViewController : UIViewController
{
    public override void ViewDidLoad ()
{
     base.ViewDidLoad ();
         //...
         this.DataTableView.Delegate = new DataTableViewSource();
    }

    public class DataTableViewSource : UITableViewSource
    {
         //UITableSource methods
    }
}

Because of this, I have to somehow pass my data to a DataViewTableSource. This works, but I prefer to manage my tabular data in the UIViewController itself (including cells, etc.). A kind of how Objective-C works with the implementation of the UITableViewDataSource protocol (interface).

Is this possible in Xamarin?

+3
source share
1 answer

This is possible if you use Xamarin.iOS 7.0 or up and make your own view constructor IUITableViewDataSourceor IUITableViewDelegate.

Example:

public class DataViewController : UIViewController, IUITableViewDataSource, IUITableViewDelegate
{
    public override void ViewDidLoad ()
    {
         base.ViewDidLoad ();
         //...
         this.DataTableView.WeakDataSource = this; //Make sure to use WeakDataSource instead of DataSource
    }

    public override int RowsInSection (UITableView tableview, int section){

    }

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {

    }
}

, Optional Objective-C, override Objective-C.

:

[Export ("tableView:accessoryButtonTappedForRowWithIndexPath:")]
public void AccessoryButtonTapped (UITableView tableView, NSIndexPath indexPath)
{
}

Xamarin Studio/Xamarin.iOS Visual Studio, intellisense .

+1

All Articles