`
yourgame
  • 浏览: 352737 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

通过编程实现UINavigationController 包含 UITableViewController

    博客分类:
  • IOS
阅读更多
//应用程序代理
@interface SimpleTableViewAppDelegate : NSObject <UIApplicationDelegate> {
	
	UIWindow *window;   //程序主窗体
	UINavigationController *navigationController; //在接口中定义导航控制器
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;

@end

//引入接口
#import "SimpleTableViewAppDelegate.h"
#import "RootViewController.h" //继承UITableViewController的子类接口


@implementation SimpleTableViewAppDelegate

@synthesize window;
@synthesize navigationController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {
		
	/*
	 通过一般样式初始化UITableViewController子类
     */	
	RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
	
    //接收一个数组,数据从当前系统获得所有时区作为数组的元素
	NSArray *timeZones = [NSTimeZone knownTimeZoneNames];
    
    //根视图控制器UITableViewController中时区名的数组数据等于排序后的时区数组数据
	rootViewController.timeZoneNames = [timeZones sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
		
    
    //------------知识点-------------
    //编程实例化一个导航控制器,并没有继承某个类或者遵循某个协议,关键是initWithRootViewController:rootViewController方法,他是把rootViewController实例作为导航控制器的根视图。
	UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    
    //赋值到当前程序代理接口中定义的导航控制器类的实体
	self.navigationController = aNavigationController;
	[aNavigationController release];
	[rootViewController release];
	
	//把导航控制器的视图增加到当前的window对象中。
	[window addSubview:[navigationController view]];
	[window makeKeyAndVisible];
}

- (void)dealloc {
	[navigationController release];
    [window release];
    [super dealloc];
}

@end

//表视图控制器子类
@interface RootViewController : UITableViewController {
	NSArray *timeZoneNames; //存储所有时区名字
}

@property (nonatomic, retain) NSArray *timeZoneNames;

@end



#import "RootViewController.h"
#import "SimpleTableViewAppDelegate.h"


@implementation RootViewController

@synthesize timeZoneNames;


- (void)viewDidLoad {
    //设定当前控制器的标题,以备导航控制器显示
    //通过第一个参数作为键取当前程序沙盒中找Localizable.strings文件中的内容,第二个参数是备注这个字段的意思,不显示到界面中的。
	self.title = NSLocalizedString(@"Time Zones", @"Time Zones title"); 
}

//接口继承自UITableViewController,需要实现这个方法获得一共有多少部分。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	// 这里只有一个部分.
	return 1;
}

//接口继承自UITableViewController,需要实现这个方法获得每一部分有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	// Return the number of time zone names.
	return [timeZoneNames count];
}

//回调方法,表格视图控制器的每一行的显示都是通过这个方法进行处理的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	
	// Try to retrieve from the table view a now-unused cell with the given identifier.
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	
	// If no cell is available, create a new one using the given identifier.
	if (cell == nil) {
		// Use the default cell style.
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
	}
	
	// 从时区数组中获得当前行号对应的时区数据作为这行的文本显示
	NSString *timeZoneName = [timeZoneNames objectAtIndex:indexPath.row];
	cell.textLabel.text = timeZoneName;
	
	return cell;
}

/*
 To conform to Human Interface Guildelines, since selecting a row would have no effect (such as navigation), make sure that rows cannot be selected.
 当对表格行进行选择时,禁止做选择动作。
 */
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	return nil;
}


- (void)dealloc {
	[timeZoneNames release];
	[super dealloc];
}
	
@end


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics