Reputation: 1632
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.
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.
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);
}
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);
}
}
Upvotes: 0
Views: 558
Reputation: 3276
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);
}
}
Upvotes: 1