Problem with UITableViewDelegate
I have the following tableviewdelegate:
private class TableViewDelegate : UITableViewDelegate
{
private DaysViewController _dvc;
private List<DateTime> ConferenceDates;
public TableViewDelegate (DaysViewController controller, List<DateTime> dates)
开发者_运维知识库 {
_dvc = controller;
ConferenceDates = dates;
}
/// <summary>
/// If there are subsections in the hierarchy, navigate to those
/// ASSUMES there are _never_ Categories hanging off the root in the hierarchy
/// </summary>
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
string date = ConferenceDates[indexPath.Row].ToShortDateString ();
Console.WriteLine ("DaysViewController:TableViewDelegate.RowSelected: Label=" + date);
/*SessionsViewController sessionsView = new SessionsViewController(date);
sessionsView.Title = date;
_dvc.NavigationController.PushViewController(sessionsView,true);*/
EventsViewController eventsView = new EventsViewController (date);
eventsView.Title = date;
try {
_dvc.NavigationController.PushViewController
(eventsView, true);
} catch (Exception ex) {
Log.Error(ex);
}
}
}
which gets added like so:
tableView = new UITableView {
Delegate = new TableViewDelegate (this, ConferenceDates),
DataSource = new TableViewDataSource (this, ConferenceDates),
AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth,
BackgroundColor = UIColor.Clear,
TableHeaderView = navBar,
Frame = new RectangleF (0, 0, this.View.Frame.Width, this.View.Frame.Height)
};
It all works well for the UITableView - the data gets loaded etc but whenever I click on a cell to load the child data - _dvc.NavigationController is null
does anyone know where this gets set? or perhaps, if it's set by the framework - how it might get nulled?
cheers - let me know if i need to post more of the code.
w://
If you are going to use a navigation controller on a view, you can accomplish this by creating an instance of a UINavigationController and add your view to this controller. Then your view becomes a sub view of the UINavigationController and your view´s NavigationController should now be accessible from within your TableViewDelegate. Present the Navigation controller the you want. In the example below, as a modal view controller.
tableView = new UITableView {
Delegate = new TableViewDelegate (this, ConferenceDates),
DataSource = new TableViewDataSource (this, ConferenceDates),
AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth,
BackgroundColor = UIColor.Clear,
TableHeaderView = navBar,
Frame = new RectangleF (0, 0, this.View.Frame.Width, this.View.Frame.Height)
};
UINavigationController navController = new UINavigationController(tableView);
navController.ModalTransitionStyle = UIModalTransitionStyle.FlipHorizontal;
this.PresentModalViewController(navController, true);
strange thing is - if i do this in the constructor:
public DVCTableViewDelegate (DaysViewController controller, List<DateTime> dates)
{
this._dvc = controller;
this.ConferenceDates = dates;
this.navController = controller.NavigationController;
}
navController is a UINavigationController but dvc.NavigationController is null in the row selected method!
strangeness.
精彩评论