0

我对 XCode 和 Monotouch 开发非常陌生。我正在尝试在根视图(主从应用程序)中添加多个选项卡。我使用 Mono 进行开发,使用 xCode 4 进行 UI 设计。例如,一个选项卡显示员工列表,另一个选项卡显示部门列表。

我删除了 rootviewController.xib 文件中的默认表格视图并替换为 tabbedview,但我无法为每个选项卡工作获得新的表格视图。

如果有人能告诉我这样做的正确方法,我将不胜感激。

更新:我使用下面的代码添加 2 个选项卡和 2 个表格视图。一个是持有“证券”,一个是持有“指标”

using System;
using System.Drawing;

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

namespace ChartQ
{
public partial class RootViewController : UITabBarController 
{
    public DetailViewController DetailVC { get; set; }

    private List<SecurityInfo> listSecInfo =  new List<SecurityInfo>();
    private List<Indicator> listIndicator =  new List<Indicator>();
    public RootViewController () : base ("RootViewController", null)
    {
        this.Title = NSBundle.MainBundle.LocalizedString ("Securities", "Securities");
        //this.ClearsSelectionOnViewWillAppear = false;
        this.ContentSizeForViewInPopover = new SizeF (100f, 200f);
        //this.ContentSizeForViewInPopover = new SizeF (320f, 600f);

        // Custom initialization
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        //Read data from Database..
        UITableViewController secController =  new UITableViewController();
        UITableViewController IndiController =  new UITableViewController();

        this.AddChildViewController(secController);
        this.AddChildViewController(IndiController);
        SQLLiteDatabase db =  new SQLLiteDatabase();
        listSecInfo =  db.ReadSecurities();
        listIndicator  = db.ReadIndicator();
        //TableView.Source = new SecuritiesDataSource (this);
        secController.TableView.Source = new SecuritiesDataSource (this);
        secController.TableView.SelectRow (NSIndexPath.FromRowSection (0, 0), false, UITableViewScrollPosition.Middle);

        IndiController.TableView.Source = new IndicatorDataSource (this);
        IndiController.TableView.SelectRow (NSIndexPath.FromRowSection (0, 0), false, UITableViewScrollPosition.Middle);


    }

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

    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 ViewDidUnload ()
    {
        base.ViewDidUnload ();


        // Clear any references to subviews of the main view in order to
        // allow the Garbage Collector to collect them sooner.
        //
        // e.g. myOutlet.Dispose (); myOutlet = null;

        ReleaseDesignerOutlets ();
    }
    class SecuritiesDataSource : UITableViewSource
    {
        static NSString cellIdentifier = new NSString ("CellId");
        RootViewController controller;

        public SecuritiesDataSource (RootViewController controller)
        {
            this.controller = controller;
        }

        // Customize the number of sections in the table view.
        public override int NumberOfSections (UITableView tableView)
        {
            return 1;
        }

        public override int RowsInSection (UITableView tableview, int section)
        {
            return controller.listSecInfo.Count;
        }

        public override string TitleForHeader (UITableView tableView, int section)
        {
            return "Securities";
        }

        // Customize the appearance of table view cells.
        public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            string cellIdentifier = "Cell";
            var cell = tableView.DequeueReusableCell (cellIdentifier);
            if (cell == null) {
                cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);
                //Add in a detail disclosure icon to each cell
                cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
            }

            // Configure the cell.
            var sInfo = controller.listSecInfo [indexPath.Row];
            cell.TextLabel.Text = String.Format ("{0}",sInfo.SecCode);
            cell.DetailTextLabel.Text = sInfo.SecName;

            return cell;
        }


        private void InfoAlert (string msg)
        {
            using (UIAlertView av = new UIAlertView("Info", msg, null, "OK", null)) {
                av.Show ();
            }
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            var sInfo = controller.listSecInfo [indexPath.Row];

            controller.DetailVC.DrawChart(sInfo.SecID, sInfo.SecCode);
        }



    }
    class IndicatorDataSource : UITableViewSource
    {
        static NSString cellIdentifier = new NSString ("CellId");
        RootViewController controller;

        public IndicatorDataSource (RootViewController controller)
        {
            this.controller = controller;
        }

        // Customize the number of sections in the table view.
        public override int NumberOfSections (UITableView tableView)
        {
            return 1;
        }

        public override int RowsInSection (UITableView tableview, int section)
        {
            return controller.listIndicator.Count;
        }

        public override string TitleForHeader (UITableView tableView, int section)
        {
            return "Indicators";
        }

        // Customize the appearance of table view cells.
        public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            string cellIdentifier = "Cell";
            var cell = tableView.DequeueReusableCell (cellIdentifier);
            if (cell == null) {
                cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);
                //Add in a detail disclosure icon to each cell
                cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
            }

            // Configure the cell.
            var sInfo = controller.listIndicator [indexPath.Row];
            cell.TextLabel.Text = String.Format ("{0}",sInfo.DescriptiveName);
            cell.DetailTextLabel.Text = sInfo.ShortName;

            return cell;
        }



        private void InfoAlert (string msg)
        {
            using (UIAlertView av = new UIAlertView("Info", msg, null, "OK", null)) {
                av.Show ();
            }
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            var sInfo = controller.listIndicator [indexPath.Row];

            //controller.DetailVC.DrawChart(sInfo.SecID, sInfo.SecCode);
        }


    }

}

但是,当我单击第二个选项卡时它会崩溃。我收到如下错误

“在执行本机代码时获得了 SIGSEGV”

第一个列表填充正常。

4

1 回答 1

0

我认为 UITableViewController 对象正在被收集并导致您中止。制作 secController 和 secController 成员变量。

public partial class RootViewController : UITabBarController
{
    UITableViewController secController;
    UITableViewController IndiController;

    public DetailViewController DetailVC { get; set; }

...

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();
    //Read data from Database..
    secController = new UITableViewController ();
    IndiController = new UITableViewController ();
于 2012-05-13T21:35:27.727 回答