2013-10-02 108 views
0

我最近进入iPhone开发并喜欢它。然而,过去几个小时,我一直在使用表格视图,更具体地说,给表格视图一个自定义高度,并在其上添加一个导航栏。Monotouch - 改变表格视图的大小和位置

在界面生成器中,我将表格视图和导航栏拖到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中更改表格的大小/位置,它仍然保持全屏显示,导航栏不会显示。

在此先感谢!

回答

0

在界面生成器中,修改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; 
     } 
相关问题