首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

公共视图的兑现

2012-06-30 
公共视图的实现?如上图所示,页面分为两个部分:上半部分是一个UIDatePicker,下半部分是一个UITableView。这

公共视图的实现

公共视图的兑现

?

如上图所示,页面分为两个部分:上半部分是一个UIDatePicker,下半部分是一个UITableView。这两部分可以同时放在同一个XIB里,但是,如果UITableView是一个公共视图的话,也就是说,其他页面也会显示这个UITableView,而且列表内容相同,那么,这样做的话,就得在每个需要显示该表视图的XIB中都加上UITableView以及相应的实现,这样就造成了冗余。

?

我们可以将这个表视图单独出来,作为公共的部分,在需要显示的地方加载进来即可。

?

下面看一个简单的示例。

?

新建一个Test项目,TestAppDelegate.h内容如下:

?

#import <UIKit/UIKit.h>@class TestViewController;@interface TestAppDelegate : NSObject <UIApplicationDelegate> {    UIWindow *window;    TestViewController *viewController;}@property (nonatomic, retain) IBOutlet UIWindow *window;@property (nonatomic, retain) IBOutlet TestViewController *viewController;@end

?
?TestAppDelegate.m内容如下:

?

#import "TestAppDelegate.h"#import "TestViewController.h"@implementation TestAppDelegate@synthesize window;@synthesize viewController;#pragma mark -#pragma mark Application lifecycle- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        self.window.rootViewController = self.viewController;    [self.window makeKeyAndVisible];    return YES;}#pragma mark -#pragma mark Memory management- (void)dealloc {    [viewController release];    [window release];    [super dealloc];}@end

?

TestViewController.xib中是一个UIDatePicker,TestViewController.h内容如下:

?

#import <UIKit/UIKit.h>#import "tableViewController.h"@interface TestViewController : UIViewController {tableViewController *tblController;}@end

?

TestViewController.m内容如下:

?

#import "TestViewController.h"#import "tableViewController.h"@implementation TestViewController- (void)viewDidLoad {    [super viewDidLoad];tblController = [[tableViewController alloc] init];CGRect rect = CGRectMake(0, 220.f, 320.f, 230.f);tblController.view.frame = rect;[self.view addSubview:tblController.view];}- (void)dealloc {[tblController release];    [super dealloc];}@end

?

tableViewController.xib中是一个UITableView,tableViewController.h内容如下:

?

#import <UIKit/UIKit.h>@interface tableViewController : UITableViewController {}@property(nonatomic, retain) NSArray *ary;@end

?

tableViewController.m内容如下:

?

#import "tableViewController.h"@implementation tableViewController@synthesize ary;#pragma mark -#pragma mark View lifecycle- (void)viewDidLoad {    [super viewDidLoad];NSArray *array = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", nil];self.ary = array;[array release];}#pragma mark -#pragma mark Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [self.ary count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {        static NSString *CellIdentifier = @"Cell";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];    }        cell.textLabel.text = [self.ary objectAtIndex:[indexPath row]];        return cell;}#pragma mark -#pragma mark Memory management- (void)dealloc {[ary release];    [super dealloc];}@end
?

热点排行