2

Monotouch を使用して iPad アプリを開発しています。

これが私のシナリオです:

タブ付きベースアプリケーションを作成しました。例: Home、Admin、Reports....etc Home タブは UIViewController です。

ホームタブ内に3つのセクションが必要です:たとえば、カテゴリ(ナビゲーションコントロール付きのテーブルビュー(カテゴリ内にサブカテゴリがあるためナビゲーションを使用する理由))、カテゴリテーブルの横、選択されたカテゴリのアイテム(その他のテーブルビュー)、右側は選択されたの詳細ビューですアイテム。

これが私がしたことです....

2 つのテーブルビュー コントローラーを動的に作成し、メイン ビュー コントローラーに追加します。

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;

    }


}

}

** * ** * ** * * ItemViewController * *

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.
    }
}

}


質問:

  • このシナリオを実行するのは正しい方法ですか?
  • 正常に動作しますが、Item ビューを親 View Controller に追加すると、アクセスできなくなります。つまり、アイテム リストは存在しますが、セルにアクセスできず、セルに触れたり、上げたりスクロールしたりすることはできません。

誰でも私を助けることができますか?

ありがとう

4

1 に答える 1

4

あなたが何をしたかったのか完全にはわかりません。最善の推測を行い、2 つのソリューションを含むサンプルを作成しました。タブごとに 1 つのソリューション。1 つ目は、組み込みの分割ビュー コントローラーを使用します。UISplitViewController がアプリケーションのルートでなければならないいくつかの場所を読みました。タブコントロールの子として追加することで、そのルールを破りました。

2 つ目は、分割コントロールのカスタム バージョンを作成します。

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 に答える