单击tableview时如何隐藏视图在Xamarin iOS中委派?

时间:2016-11-28 12:35:10

标签: ios xamarin xamarin.ios tableview

我正在使用Xamarin iOS平台,我打开了一个TableView的视图。我希望在RowSelected委托方法调用

时隐藏弹出窗口视图

这是我的代码:

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();
}

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);
}

1 个答案:

答案 0 :(得分:1)

从DataSource隐藏ViewController中的视图,将ViewController的实例传递给DataSource(通过construtor)

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

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

然后在RowSelected中调用HidePopup方法。

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