Rahul Anand Rahul Anand - 9 days ago 6
iOS Question

How to hide view when click on tableview Delegate in Xamarin iOS?

I am working on Xamarin iOS platform and I have open one view having

TableView
. I want to hide popup view when
RowSelected
delegate method call

Here is my code:

In
TableDatasource.cs
:

using UIKit;


namespace NewProject.IOS
{
public class MapFilterDataSource : UITableViewSource
{
MainViewController controller;
protected string[] tableItems;
protected string cellIdentifier = "TableCell";
MapViewController owner;
public static string StrSelectedRow = "Intial";
int index = -1;

string[] arrCrimePerson = new string[] { "asd", "asd", "Acte asd", "asd", "a", "asd" };
string[] arrCrimeProperie = new string[]{ "asd", "asd", "Acte asd", "asd", "a", "asd" };
string[] arrStupefiants = new string[] { "asd", "asd", "Acte asd", "asd", "a", "asd" };

public MapFilterDataSource(string[] items, MapViewController owner)
{
tableItems = items;
this.owner = owner;

}

public override nint NumberOfSections(UITableView tableView)
{
return 1;
}

public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
MapViewController.StrSelectedValue = "Autre";
MapViewController obj = new MapViewController();
obj.HidePopUp();
}


In
MainViewController.cs
:

public override async void ViewDidLoad()
{
base.ViewDidLoad ();
CreateTableItems();
}

public virtual void HidePopUp()
{
Debug.WriteLine("Button " + StrSelectedValue + " clicked");

viewPopUp.RemoveFromSuperview();
// This viewPopUp i want to hide but its crashing here showing error like "system.argumentnullexception value cannot be null"
}

protected void CreateTableItems()
{
List<string> tableItems = new List<string>();
tableItems.Add("asd");
tableItems.Add("sdf");
tableItems.Add("sdf");
tableItems.Add("aaa");

tblFilter.TableFooterView = new UIView(new CGRect(0, 0, 0, 0));
tblFilter.Source = new MapFilterDataSource(tableItems.ToArray(), this);
}

Answer

to hide a view in your ViewController from the DataSource, pass your ViewController's instance to the DataSource (via construtor)

public class MyDataSource : UITableViewSource
{
    MainViewController controller;
    List<string> tableItems

    public ProgramArticlesTableSource (List<string> tableItems, MainViewController controller)
    {
        this.tableItems = tableItems;
        this.controller = controller;
    }
    ......

then call HidePopup method in RowSelected.

public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
    controller.HidePopUp();
}