第一個(gè)項(xiàng)目之三--UITableView

再接再厲


下面就要開始敲代碼啦馍资!
先建一個(gè)分組筒主,恩,為了美觀鸟蟹。
<br />

RootViewController



選中NEW GROUP
我給它命名為GarbageCenter乌妙,后面的全局類我都會(huì)扔在這里,垃圾回收中心建钥!
按下Command+N藤韵,制造一個(gè)新朋友Cocoa touch class。并給它一個(gè)名字:RootViewController熊经,并讓其繼承UIViewController泽艘。

Paste_Image.png

不打算用XIB開發(fā),所以就把鉤去了就好了镐依!
Language默認(rèn)Objective-C就好了匹涮。

Paste_Image.png

恭喜你,第一個(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
運(yùn)行結(jié)果

這樣勇劣,一個(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è)效果:

運(yùn)行結(jié)果

下面就是將這個(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)在效果就是這樣了:

運(yùn)行結(jié)果

<br />

最后


暫時(shí)就到這里吧醋奠,下篇就到了事件監(jiān)聽了瓮下。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市钝域,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌锭魔,老刑警劉巖例证,帶你破解...
    沈念sama閱讀 219,366評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異迷捧,居然都是意外死亡织咧,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門漠秋,熙熙樓的掌柜王于貴愁眉苦臉地迎上來笙蒙,“玉大人,你說我怎么就攤上這事庆锦⊥蔽唬” “怎么了?”我有些...
    開封第一講書人閱讀 165,689評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵搂抒,是天一觀的道長艇搀。 經(jīng)常有香客問我,道長求晶,這世上最難降的妖魔是什么焰雕? 我笑而不...
    開封第一講書人閱讀 58,925評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮芳杏,結(jié)果婚禮上矩屁,老公的妹妹穿的比我還像新娘。我一直安慰自己爵赵,他們只是感情好吝秕,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著亚再,像睡著了一般郭膛。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上氛悬,一...
    開封第一講書人閱讀 51,727評(píng)論 1 305
  • 那天则剃,我揣著相機(jī)與錄音,去河邊找鬼如捅。 笑死棍现,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的镜遣。 我是一名探鬼主播己肮,決...
    沈念sama閱讀 40,447評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼士袄,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了谎僻?” 一聲冷哼從身側(cè)響起娄柳,我...
    開封第一講書人閱讀 39,349評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎艘绍,沒想到半個(gè)月后赤拒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,820評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡诱鞠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評(píng)論 3 337
  • 正文 我和宋清朗相戀三年挎挖,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片航夺。...
    茶點(diǎn)故事閱讀 40,127評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蕉朵,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出阳掐,到底是詐尸還是另有隱情始衅,我是刑警寧澤,帶...
    沈念sama閱讀 35,812評(píng)論 5 346
  • 正文 年R本政府宣布锚烦,位于F島的核電站觅闽,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏涮俄。R本人自食惡果不足惜蛉拙,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望彻亲。 院中可真熱鬧孕锄,春花似錦、人聲如沸苞尝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宙址。三九已至轴脐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間抡砂,已是汗流浹背大咱。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留注益,地道東北人碴巾。 一個(gè)月前我還...
    沈念sama閱讀 48,388評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像丑搔,于是被迫代替她去往敵國和親厦瓢。 傳聞我的和親對(duì)象是個(gè)殘疾皇子提揍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容