再接再厲
下面就要開始敲代碼啦馍资!
先建一個(gè)分組筒主,恩,為了美觀鸟蟹。
<br />
RootViewController
選中NEW GROUP
我給它命名為GarbageCenter乌妙,后面的全局類我都會(huì)扔在這里,垃圾回收中心建钥!
按下Command+N藤韵,制造一個(gè)新朋友Cocoa touch class。并給它一個(gè)名字:RootViewController熊经,并讓其繼承UIViewController泽艘。
不打算用XIB開發(fā),所以就把鉤去了就好了镐依!
Language默認(rèn)Objective-C就好了匹涮。
恭喜你,第一個(gè)類這樣就創(chuàng)建完成了槐壳!
這時(shí)候按下Command+R然低,項(xiàng)目就可以跑起來了。不過到目前為止,項(xiàng)目還是會(huì)以StoryBoard的方式開啟雳攘,那剛才新建的類要怎么用呢带兜?不用急,慢慢來吨灭,耐心刚照。
<br />
代碼方式Launch
我們重新回到AppDelegate里面。前面說過喧兄,在AppDelegate里面有個(gè)方法:didFinishLaunchingWithOptions:无畔,它是在APP啟動(dòng)完畢后執(zhí)行的方法。
而在APP啟動(dòng)之后繁莹,我們當(dāng)然是需要讓APP的窗口展示到屏幕上檩互,此時(shí)我們就要這么做:
#import "RootViewController.h"
//先導(dǎo)入剛才新建的類
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//將窗口的大小設(shè)為和屏幕大小一樣
self.window.backgroundColor = [UIColor grayColor];
//將窗口的背景顏色更改為灰色
RootViewController *instance_rootView = [[RootViewController alloc]init];
//初始化RootViewController
[instance_rootView.view setFrame:self.window.frame];
//設(shè)置RootViewController的大小和window的相同
self.window.rootViewController = instance_rootView;
//將RootViewController設(shè)置為根視圖控制器
[self.window makeKeyAndVisible];
//然后設(shè)置窗口為可見
return YES;
}
這是再Run項(xiàng)目之后,就不再是以StoryBoard的形式啟動(dòng)了咨演。進(jìn)入APP之后闸昨,看到的就是RootViewController的一片白茫茫。下面就到RootViewController了薄风。
測試菜單--列表
這個(gè)項(xiàng)目的目的是為了集成一大堆測試內(nèi)容而建的饵较,所以首頁就應(yīng)該做成一個(gè)菜單。而在UIKit里面遭赂,就有一個(gè)很適合的類庫循诉,UITableView,它幾乎穿越了整個(gè)APP的開發(fā)周期撇他,到處都到得到他茄猫。
我們先來看看例子。
#import "RootViewController.h"
//拓展了兩個(gè)協(xié)議困肩,UITableViewDataSource和UITableViewDelegate
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
//初始化一個(gè)UITableView
@property (nonatomic, strong) UITableView *view_table;
@end
@implementation RootViewController
//頁面加載的時(shí)候會(huì)執(zhí)行這個(gè)方法
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initRootView]; //調(diào)用自定義初始化RootView方法
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
//初始化RootView里面的內(nèi)容
- (void)initRootView {
self.view.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1]; //設(shè)置背景顏色
self.view_table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; //初始化UITableView划纽,并設(shè)置起大小和RootView的大小一樣,同時(shí)將UITableVIew的樣式改為分組樣式
self.view_table.delegate = self;
self.view_table.dataSource = self;
//設(shè)置UITableView的代理和數(shù)據(jù)源為RootView
[self.view addSubview:self.view_table]; //將UITableView添加到RootView上顯示
}
//該方法適用于UITableViewStyleGrouped锌畸,返回分組數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//該方法會(huì)返回每個(gè)組數(shù)里面包含的行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
//返回每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 45;
}
//返回每行的對(duì)象
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell_temp = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell%ld0%ld", indexPath.section, indexPath.row]];
cell_temp.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell_temp;
}
@end
這樣勇劣,一個(gè)只有單行單組的表格視圖就完成了!大功告成潭枣。
因?yàn)槭莻€(gè)菜單嘛比默,只有一行怎么能行呢?先加上目錄兩個(gè)字吧盆犁!
- (void)initTableHeaderView{
UIView *view_header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
UILabel *lb_menu = [[UILabel alloc]initWithFrame:view_header.frame];
lb_menu.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
lb_menu.textAlignment = NSTextAlignmentCenter;
lb_menu.font = [UIFont fontWithName:@"Arial" size:16.0];
lb_menu.text = @"目 錄";
[view_header addSubview:lb_menu];
self.view_table.tableHeaderView = view_header;
}
然后我們就會(huì)看到這個(gè)效果:
下面就是將這個(gè)首頁命咐,稍微打包打包,讓后面添加新的分組和行數(shù)據(jù)時(shí)能夠方便點(diǎn)谐岁。
改動(dòng)后的代碼:
#import "RootViewController.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *view_table;
@property (nonatomic, strong) NSArray *array_section;
@property (nonatomic, strong) NSArray *array_row;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initRootView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (void)initRootView {
self.view.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
self.view_table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.view_table.delegate = self;
self.view_table.dataSource = self;
[self.view addSubview:self.view_table];
[self initTableHeaderView];
[self initData];
}
- (void)initTableHeaderView{
UIView *view_header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
UILabel *lb_menu = [[UILabel alloc]initWithFrame:view_header.frame];
lb_menu.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
lb_menu.textAlignment = NSTextAlignmentCenter;
lb_menu.font = [UIFont fontWithName:@"Arial" size:16.0];
lb_menu.text = @"目 錄";
[view_header addSubview:lb_menu];
self.view_table.tableHeaderView = view_header;
}
- (void)initData{
self.array_section = [NSArray arrayWithObjects:@"第一章", nil];
self.array_row = [NSArray arrayWithObjects:
[NSArray arrayWithObjects:@"第一節(jié)", @"第二節(jié)", nil],
nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.array_section count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
switch (section) {
case 0:
return [self.array_row[section] count];
break;
default:
return 0;
break;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 45;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.array_section[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell_temp = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell%ld0%ld", indexPath.section, indexPath.row]];
cell_temp.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell_temp.selectionStyle = UITableViewCellSelectionStyleNone;
cell_temp.textLabel.text = [self.array_row[indexPath.section] objectAtIndex:indexPath.row];
return cell_temp;
}
@end
現(xiàn)在效果就是這樣了:
<br />
最后
暫時(shí)就到這里吧醋奠,下篇就到了事件監(jiān)聽了瓮下。