UITableViewDelegate的问题

时间:2010-02-28 16:41:00

标签: c# iphone cocoa-touch mono xamarin.ios

我有以下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);
            }
        }
    }

这样添加:

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

这一切都适用于UITableView - 数据加载等但每当我点击一个单元格来加载子数据时_dvc.NavigationController为空

有谁知道这个设置在哪里?或者,如果它是由框架设定的 - 它如何被取消?

欢呼 - 如果我需要发布更多代码,请告诉我。

瓦特://

2 个答案:

答案 0 :(得分:2)

如果要在视图上使用导航控制器,可以通过创建UINavigationController的实例并将视图添加到此控制器来完成此操作。然后您的视图成为UINavigationController的子视图,您的视图的NavigationController现在应该可以从TableViewDelegate中访问。提供您想要的导航控制器。在下面的示例中,作为模态视图控制器。

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

答案 1 :(得分:1)

奇怪的是 - 如果我在构造函数中执行此操作:

public DVCTableViewDelegate (DaysViewController controller, List<DateTime> dates)
{
    this._dvc = controller;
    this.ConferenceDates = dates;
    this.navController = controller.NavigationController;
}

navController是一个UINavigationController,但dvc.NavigationController在行选择方法中为空!

不协调。