我最近涉足 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# 类。
通用表.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;
}
}
}
主屏幕.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 中更改表格的大小/位置,它仍然保持全屏状态,并且导航栏不会出现。
提前致谢!