Xamrin iOS - 在列表视图 RowSelected 打开视图控制器 [英] Xamrin iOS - On list view RowSelected open view controller

查看:22
本文介绍了Xamrin iOS - 在列表视图 RowSelected 打开视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to iOS development, I googled for my problem but most of the solutions are for Swift 3. I am developing my application in Xamarin Native iOS.

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

I have a UITableView having a list of data. All I need to do is when ever I click a row, the event will open a new page through ViewController which will display detailed information of the selected data.

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

Here is my code for UITableView in PlayersListController.cs file:

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 类

Here is my model class 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);

}

}

推荐答案

你的 UITableViewSource 通常不会有一个指向导航控制器的指针,所以在大多数情况下有必要提供它.下面的代码是做你想做的事情的一个非常基本的方法.

Your UITableViewSource won't usually have a pointer to the navigation controller, so in most instances it's necessary to provide that. The following code is a very basic way of doing what you want.

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

然后在您的源代码中,您只想保留该导航控制器指针以用于行选择.

and then in your source you just want to keep hold of that navigation controller pointer for use on row select.

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

这篇关于Xamrin iOS - 在列表视图 RowSelected 打开视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆