Xamarin iOS - 在列表视图行选择打开视图控制器
Posted
技术标签:
【中文标题】Xamarin iOS - 在列表视图行选择打开视图控制器【英文标题】:Xamrin iOS - On list view RowSelected open view controller 【发布时间】:2017-05-25 08:03:51 【问题描述】:我是 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 类
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】:您的 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);
【讨论】:
我使用了你的代码。在以下行:PlayerDetailController PlayerDetailController = this.Storyboard.InstantiateViewController("PlayerDetailController") as PlayerDetailController;
我收到错误“不包含情节提要的定义并且没有扩展方法..”
@zainSMJ 道歉,应该是 primNav.Storyboard 我已经更改了上面的代码以反映这一点。
完美运行 :) 谢谢以上是关于Xamarin iOS - 在列表视图行选择打开视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
自定义标签渲染器在 Xamarin iOS 的滚动列表视图中显示错误的文本样式