2

我正在使用 Monotouch 开发 iPad 应用程序。

这是我的场景:

我创建了选项卡式基础应用程序。例如 Home、Admin、Reports....等 Home 选项卡是 UIViewController。

我想要主页选项卡内的三个部分:例如类别(带有导航控件的表视图(使用导航的原因,因为我们在类别中有子类别),在类别表旁边,选定类别的项目(其他表视图)和右侧是选定的详细视图物品。

这就是我所做的......

动态创建两个 tableview 控制器并添加到主视图控制器。

HomeViewController.cs:

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

         RootViewController rvc = new RootViewController("",UITableViewStyle.Grouped);

        // navigation controller will manage the views displayed and provide navigation buttons
        navigationController = new UINavigationController();
        navigationController.PushViewController(rvc, false);
        navigationController.TopViewController.Title ="Category";

        navigationController.View.Frame = new RectangleF (0, 50, (50), (600));



        // Main window to which we add the navigation controller to
         this.View.AddSubview(navigationController.View);

        itemtable.Delegate = new TableViewDelegate (list);
        itemtable.DataSource = new TableViewDataSource (list);

        // Perform any additional setup after loading the view, typically from a nib.
    }

==================================================== ===

根视图控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;

namespace DVPNTN_MobileApp
{
[MonoTouch.Foundation.Register("RootViewController")]
public partial class RootViewController : UITableViewController
{
    public List<string> RootData = new List<string> { "Group1", "Group2" };
    MonoTouch.UIKit.UINavigationController navigationControllerItem;
    string SelectedGroup;

    // Allow us to set the style of the TableView
    public RootViewController(string selectedGroup, UITableViewStyle style) : base(style)
    {
        this.SelectedGroup = selectedGroup; 
    }


    class DataSource : UITableViewDataSource
    {
        static NSString kCellIdentifier = new NSString ("MyIdentifier");
        RootViewController tvc;

        public DataSource (RootViewController tvc)
        {
            this.tvc = tvc;
        }

        public override int RowsInSection (UITableView tableView, int section)
        {
            return tvc.RootData.Count;
        }

        public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.DequeueReusableCell (kCellIdentifier);

            if (cell == null)
            {
                cell = new UITableViewCell (UITableViewCellStyle.Default, kCellIdentifier);
            }

            cell.TextLabel.Text = tvc.RootData.ElementAt(indexPath.Row);
            cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
            return cell;
        }
    }

    class TableDelegate : UITableViewDelegate
    {
        RootViewController tvc;
        SubGroupViewController sgvc;

        public TableDelegate (RootViewController tvc)
        {
            this.tvc = tvc;
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            string selectedGroup = tvc.RootData.ElementAt(indexPath.Row);

            sgvc = new SubGroupViewController(selectedGroup, UITableViewStyle.Grouped);

            tvc.NavigationController.PushViewController(sgvc,true);
            //tvc.View.RemoveFromSuperview();
            //tvc.DidReceiveMemoryWarning();
            GC.Collect();
        }
    }

    public override void DidReceiveMemoryWarning ()
    {
        // Releases the view if it doesn't have a superview.
        base.DidReceiveMemoryWarning ();

        // Release any cached data, images, etc that aren't in use.
    }
    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        TableView.Delegate = new TableDelegate (this);
        TableView.DataSource = new DataSource (this);


        RootVIewItemController rvc1 = new RootVIewItemController(SelectedGroup,UITableViewStyle.Grouped);

        // navigation controller will manage the views displayed and provide navigation buttons
        navigationControllerItem = new UINavigationController();

        navigationControllerItem.PushViewController(rvc1, false);
        navigationControllerItem.TopViewController.Title = SelectedGroup + " " + "Item List";


            navigationControllerItem.View.Frame = new RectangleF (0, 300, (50),(700));

         //this.View.AddSubview(navigationControllerItem.View);
        //rvc1.View.EnableInputClicksWhenVisible = true;

        //this.ParentViewController.AddChildViewController(navigationControllerItem);
**> Problem is here --- subview is successfully added to parent view but it's not accessible, mean items are there but we can't touch cell or row???????**
        ParentViewController.View.AddSubview(navigationControllerItem.View);




        GC.Collect();
    }
    public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
    {
        // Return true for supported orientations
        return true;

    }


}

}

** * ** * ** **项目视图控制器 * *

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace DVPNTN_MobileApp
{
[MonoTouch.Foundation.Register("RootVIewItemController")]
public partial class RootVIewItemController : UITableViewController
{

    public List<string> RootData = new List<string> { "Item 1", "Item 2", "Item 3", "Item 4" };
    string SelectedGroup;

    public RootVIewItemController (string selectedGroup, UITableViewStyle style) : base (style)
    {
        this.SelectedGroup = selectedGroup; 
    }


    class DataSource : UITableViewDataSource
    {
        static NSString kCellIdentifier = new NSString ("MyIdentifier");
        RootVIewItemController tvc;

        public DataSource (RootVIewItemController tvc)
        {
            this.tvc = tvc;
        }

        public override int RowsInSection (UITableView tableView, int section)
        {
            return tvc.RootData.Count;
        }

        public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.DequeueReusableCell (kCellIdentifier);

            if (cell == null)
            {
                cell = new UITableViewCell (UITableViewCellStyle.Default, kCellIdentifier);
            }

            cell.TextLabel.Text = tvc.RootData.ElementAt(indexPath.Row);
            //cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;

            return cell;
        }
    }

    class TableDelegate : UITableViewDelegate
    {
        RootVIewItemController tvc;
        SubGroupViewController sgvc;

        public TableDelegate (RootVIewItemController tvc)
        {
            this.tvc = tvc;
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            string selectedGroup = tvc.RootData.ElementAt(indexPath.Row);
            Console.WriteLine(
                "TableViewDelegate.RowSelected: Label={0}",selectedGroup);

            /*
            if(sgvc == null)
                sgvc = new SubGroupViewController(selectedGroup, UITableViewStyle.Grouped);

            tvc.NavigationController.PushViewController(sgvc,true);*/
        }
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        TableView.Delegate = new TableDelegate (this);
        TableView.DataSource = new DataSource (this);
    }

    public override void DidReceiveMemoryWarning ()
    {
        // Releases the view if it doesn't have a superview.
        base.DidReceiveMemoryWarning ();

        // Release any cached data, images, etc that aren't in use.
    }
}

}


问题:

  • 这是做这种情况的正确方法吗?
  • 它工作正常,但是当我们将项目视图添加到父视图控制器时,它无法访问,意味着项目列表在那里但单元格不可访问,我们无法触摸单元格并提升甚至滚动或滚动。

请问有人可以帮助我吗?

谢谢

4

1 回答 1

4

我不完全确定你想做什么。我做了最好的猜测,并创建了一个包含两种解决方案的样本。每个选项卡一个解决方案。第一个使用内置的拆分视图控制器。我已经阅读了几个地方 UISplitViewController 必须是应用程序的根。我通过将其添加为选项卡控件的子项来打破该规则。

第二个创建拆分控件的自定义版本。

using System;
using System.Collections.Generic;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;

namespace delete04223
{
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        // class-level declarations
        UIWindow window;
        UITabBarController tabBarController;

        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            // create a new window instance based on the screen size
            window = new UIWindow (UIScreen.MainScreen.Bounds);

            var viewController1 = new MyUISplitViewController ();
            var viewController2 = new MyUISplitViewController2 ();
            tabBarController = new UITabBarController ();
            tabBarController.ViewControllers = new UIViewController [] {
                viewController1,
                viewController2,
            };

            window.RootViewController = tabBarController;
            // make the window visible
            window.MakeKeyAndVisible ();

            return true;
        }
    }

    public class MyUISplitViewController : UISplitViewController
    {
        public MyUISplitViewController ()
        {
            this.Title = "Native Split View";
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            var viewController1 = new LeftViewController ();
            var viewController2 = new DummyViewController ("Pane 1", "Pane 1");
            this.ViewControllers = new UIViewController [] {viewController1, viewController2};
            this.WeakDelegate = viewController2;

        }

        public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
        {
            return true;
        }
    }

    public class MyUISplitViewController2 : UIViewController
    {
        public MyUISplitViewController2 ()
        {
            this.Title = "Custom Split View";
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            var viewController1 = new LeftViewController ();
            var viewController2 = new DummyViewController ("Pane 1", "Pane 1");

            this.AddChildViewController (viewController1);
            this.AddChildViewController (viewController2);

            this.View.AddSubview (viewController1.View);
            this.View.AddSubview (viewController2.View);
        }

        public override void ViewDidLayoutSubviews ()
        {
            base.ViewDidLayoutSubviews ();

            RectangleF lRect = this.View.Frame;
            RectangleF rRect = lRect;

            lRect.Width = .3f * lRect.Width;
            rRect.X = lRect.Width + 1;
            rRect.Width = (.7f * rRect.Width)-1;

            this.View.Subviews[0].Frame = lRect;
            this.View.Subviews[1].Frame = rRect;
        }

        public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
        {
            return true;
        }
    }

    public class LeftViewController : UINavigationController
    {
        public LeftViewController ()
        {

        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();    

            MyUITableViewController table = new MyUITableViewController (this);

            this.PushViewController (table, false);
        }

        public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
        {
            return true;
        }

        public override UIViewController PopViewControllerAnimated (bool animated)
        {
            return base.PopViewControllerAnimated (true);
        }
    }

    public class DummyViewController : UIViewController
    {
        string _myLabelText = "";
        UIToolbar _toolbar;


        public DummyViewController (string viewName, string labelText)
        {
            this.Title = viewName;  
            this._myLabelText = labelText;
            this.View.BackgroundColor = UIColor.White;
        }

        public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
        {
            return true;
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            float center = this.View.Frame.Width / 2f;
            UILabel label = new UILabel (new RectangleF (center - 50, 100, 100, 40));
            label.Text = this._myLabelText;
            label.TextAlignment = UITextAlignment.Center;
            label.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;

            RectangleF rect = this.View.Frame;
            rect.Y = 0;
            rect.Height = 44;
            _toolbar = new UIToolbar (rect);
            _toolbar.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;

            this.View.AddSubview (label);
            this.View.AddSubview (_toolbar);
        }

        [Export("splitViewController:willHideViewController:withBarButtonItem:forPopoverController:")]
        public void WillHideViewController (UISplitViewController svc, UIViewController vc,
            UIBarButtonItem barButtonItem, UIPopoverController pc)
        {
            barButtonItem.Title = "Menu";
            var items = new List<UIBarButtonItem> ();
            items.Add (barButtonItem);
            if (_toolbar.Items != null) 
                items.AddRange (_toolbar.Items);
            _toolbar.SetItems (items.ToArray (), true);
            //popoverController = pc;
        }

        [Export("splitViewController:willShowViewController:invalidatingBarButtonItem:")]
        public void WillShowViewController (UISplitViewController svc, UIViewController vc,
            UIBarButtonItem button)
        {
            // Called when the view is shown again in the split view, invalidating the button and popover controller.
            var items = new List<UIBarButtonItem> (_toolbar.Items);
            items.RemoveAt (0);
            _toolbar.SetItems (items.ToArray (), true);
            //popoverController = null;
        }
    }

    internal class MyUITableViewController : UITableViewController
    {
        static NSString kCellIdentifier = new NSString ("MyIdentifier");
        LeftViewController _parent;

        public MyUITableViewController (LeftViewController parent) : base (UITableViewStyle.Plain)
        {
            this.TableView.WeakDelegate = this;
            this.TableView.WeakDataSource = this;
            this._parent = parent;
        }

        [Export ("tableView:numberOfRowsInSection:")]
        public int RowsInSection (UITableView tableView, int section)
        {
            if (section == 0)
                return 2;

            return 3;
        }

        [Export ("tableView:cellForRowAtIndexPath:")]
        public UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.DequeueReusableCell (kCellIdentifier);

            if (cell == null)
            {
                cell = new UITableViewCell (UITableViewCellStyle.Default, kCellIdentifier);
            }

            cell.TextLabel.Text = GetRowName (indexPath);

            return cell;
        }

        private string GetRowName (NSIndexPath indexPath)
        {
            string ret;
            if (indexPath.Section == 0)
            {
                ret = 
                    indexPath.Row == 0 ? "Row A" : "Row B";
            }
            else
            {
                ret = 
                    indexPath.Row == 0 ? "Row D" : 
                        indexPath.Row == 1 ? "Row E" : "Row F"; 
            }   

            return ret;
        }

        [Export ("numberOfSectionsInTableView:")]
        public int NumberOfSections (UITableView tableView)
        {
            return 2;
        }

        [Export ("tableView:titleForHeaderInSection:")]
        public string TitleForHeader (UITableView tableView, int section)
        {
            if (section == 0)
                return "One";

            return "Two";
        }

        [Export ("tableView:didSelectRowAtIndexPath:")]
        public virtual void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            Console.WriteLine("Item Selected: Section={0}, Row={1}",indexPath.Section, indexPath.Row);  
            this._parent.PushViewController (new MySubUITableViewController (), true);
        }

        public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
        {
            return true;
        }
    }

    internal class MySubUITableViewController : UITableViewController
    {
        static NSString kCellIdentifier = new NSString ("MyIdentifier");
        static string [] _names = new string [] {"One", "Two", "Three", "Four", "Five"};

        public MySubUITableViewController () : base (UITableViewStyle.Plain)
        {
            this.TableView.WeakDelegate = this;
            this.TableView.WeakDataSource = this;
        }

        [Export ("tableView:numberOfRowsInSection:")]
        public int RowsInSection (UITableView tableView, int section)
        {
            return 5;
        }

        [Export ("tableView:didSelectRowAtIndexPath:")]
        public virtual void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            Console.WriteLine("Item Selected: Section={0}, Row={1}",indexPath.Section, indexPath.Row);  
        }

        [Export ("tableView:cellForRowAtIndexPath:")]
        public UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {

            var cell = tableView.DequeueReusableCell (kCellIdentifier);

            if (cell == null)
            {
                cell = new UITableViewCell (UITableViewCellStyle.Default, kCellIdentifier);
            }

            cell.TextLabel.Text = _names[indexPath.Row];

            return cell;
        }

        public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
        {
            return true;
        }
    }
}
于 2012-04-23T05:14:42.050 回答