Monotouch - 改变桌面视图的大小和位置

时间:2013-10-02 00:09:11

标签: c# ios uitableview xamarin.ios xamarin

我最近进入iPhone开发并喜欢它。但是,在过去的几个小时里,我一直在努力处理表格视图,更具体地说,为表格视图提供自定义高度并在其上方添加导航栏。

在界面构建器中,我将表视图和导航栏拖到iPhone的“屏幕”上。然后我把它们连接起来作为插座。

来自xib文件的MainScreen.h:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>


@interface MainScreen : UIViewController 
{
    UINavigationBar *_nbMainScreen;
    UITableView *_tblMainScreen;
}

@property (retain, nonatomic) IBOutlet UINavigationBar *nbMainScreen;
@property (nonatomic, retain) IBOutlet UITableView *tblMainScreen;

@end

我为表数据创建了一个单独的C#类。

GenericTable.cs:

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

namespace TestProject
{
    public class GenericTable : UITableViewSource
    {
        protected string[] tableItems;
        protected string cellIdentifier = "TableCell";

        public GenericTable (string[] items)
        {
            tableItems = items;
        }


        public override int NumberOfSections (UITableView tableView)
        {
            return 1;
        }


        public override int RowsInSection (UITableView tableview, int section)
        {
            return tableItems.Length;
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            new UIAlertView("Row Selected"
                    , tableItems[indexPath.Row], null, "OK", null).Show();
            tableView.DeselectRow (indexPath, true);
        }


        public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
            string item = tableItems[indexPath.Row];

            //---- if there are no cells to reuse, create a new one
        if (cell == null)
        { 
                     cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier); }

                 cell.TextLabel.Text = item;

                 return cell;
        }
    }
}

MainScreen.cs:

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

namespace TestProject
{
public partial class MainScreen : UIViewController
{
    static bool UserInterfaceIdiomIsPhone {
        get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
    }

    public MainScreen ()
        : base (UserInterfaceIdiomIsPhone ? "MainScreen_iPhone" : "MainScreen_iPad", null)
    {
    }

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

        tblMainScreen = new UITableView(View.Bounds);
        tblMainScreen.AutoresizingMask = UIViewAutoresizing.All;
        CreateTableItems();
        Add (tblMainScreen);




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


    protected void CreateTableItems ()
    {
        List<string> tableItems = new List<string> ();
        tableItems.Add ("Dog");
        tableItems.Add ("Cat");
        tableItems.Add ("Plane");
        tableItems.Add ("Phone");
        tableItems.Add ("Baloon");
        tblMainScreen.Source = new GenericTable(tableItems.ToArray());
    }
}

}

这是我在多个论坛和教程网站中找到的常见策略,但所有这些策略都涉及全屏表视图。截至目前,我的表格填充正确,但是,就像在论坛帖子中一样,它会占用整个屏幕。即使我更改了IB中表格的大小/位置,它仍然保持全屏,并且不会出现导航栏。

提前致谢!

1 个答案:

答案 0 :(得分:0)

在Interface Builder中,修改TopBar属性.. 当你的UIViewController被选中时。

应该在那个带有“盾状”徽章的面板上。

之后,将AppDelegate文件修改为类似我在下面列出的代码:

enter image description here

        MainScreen controller;
        UINavigationController navcontroller;

        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {

            // create a new window instance based on the screen size
            window = new UIWindow (UIScreen.MainScreen.Bounds);
            controller = new MainScreen ();

            var initialControllers = new List<UIViewController>();
            initialControllers.Add (controller);

            navcontroller = new UINavigationController ();
            navcontroller.ViewControllers = initialControllers.ToArray ();
            window.RootViewController = navcontroller;
            window.MakeKeyAndVisible ();
            return true;
        }