Xamrin iOS - 列表视图RowSelected打开视图控制器

时间:2017-05-25 08:03:51

标签: ios uitableview xamarin.ios storyboard viewcontroller

我是iOS开发的新手,我搜索了我的问题,但大多数解决方案都是针对Swift 3.我正在Xamarin Native iOS中开发我的应用程序。

我有一个包含数据列表的UITableView。我需要做的就是当我单击一行时,事件将通过ViewController打开一个新页面,它将显示所选数据的详细信息。

这是我在PlayersListController.cs文件中的UITableView代码:

public override void ViewDidLoad() {
    base.ViewDidLoad();
    // Perform any additional setup after loading the view, typically from a nib.

    // THIS data GOES TO "PlayersListTableSource" CLASSS
    string[] data = new string[] {
        "Player One",
        "Player Two",
        "Player Three",
        "Player Four",
        "Player Five"
    };


    UITableView myTable;

    myTable = new UITableView
    {
        Frame = new CoreGraphics.CGRect(0, 30, View.Bounds.Width, View.Bounds.Height),
        Source = new PlayersListTableSource(data)
    };

    View.AddSubview(myTable);
}

这是我的模型类PlayersListTableSource.cspublic class

PlayersListTableSource : UITableViewSource
{
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
// WHAT TO DO HERE…

new UIAlertView("Alert", "You touched: " + tableItems[indexPath.Row], null, "Ok", null).Show();

tableView.DeselectRow(indexPath, true);

// PlayerDetailController is my view controller I want to open on button click

// SOME THING LIKE THIS

PlayerDetailController PlayerDetailController = this.Storyboard.InstantiateViewController("PlayerDetailController") as PlayerDetailController;

this.NavigationController.PushViewController(PlayerDetailController, true);

}

}

1 个答案:

答案 0 :(得分:1)

您的UITableViewSource通常不会有指向导航控制器的指针,所以在大多数情况下都需要提供。以下代码是做你想做的事情的一种非常基本的方式。

public override void ViewDidLoad() 
{
    base.ViewDidLoad();
    // Perform any additional setup after loading the view, typically from a nib.

    // THIS data GOES TO "PlayersListTableSource" CLASSS
    string[] data = new string[] {
        "Player One",
        "Player Two",
        "Player Three",
        "Player Four",
        "Player Five"
    };


    UITableView myTable;

    myTable = new UITableView
    {
        Frame = new CoreGraphics.CGRect(0, 30, View.Bounds.Width, View.Bounds.Height),
        Source = new PlayersListTableSource(data, this.NavigationController)
    };

    View.AddSubview(myTable);
}

然后在你的源代码中你只想保留那个导航控制器指针,用于行选择。

PlayersListTableSource : UITableViewSource
{
    private UINavigationController primNav {get; set;}


    public PlayersListTableSource(List<Object> myData, UINavigationController nav)
    {
        //DO what you want with your data

        primNav = nav;
    }

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        new UIAlertView("Alert", "You touched: " + tableItems[indexPath.Row], null, "Ok", null).Show();

        tableView.DeselectRow(indexPath, true);

        // PlayerDetailController is my view controller I want to open on button click

        // SOME THING LIKE THIS

        PlayerDetailController PlayerDetailController = primNav.Storyboard.InstantiateViewController("PlayerDetailController") as PlayerDetailController;

        primNav.PushViewController(PlayerDetailController, true);
    }
}