テーブルを作成する

objective-cは、まだどうも違和感がある。
javaはもちろんcともだいぶ異なるような。

一歩ずつ。一歩ずつ。

今日はテーブルを作成してみる。

まずは、Navigation-based Application を利用する。

今回の目標は、テーブルにセクションを付けて表示するだけ。
データはこんな感じをイメージ。

29日:Cold Play,CSS
30日:faces
31日:cake,envy

と、フジロック'11のアーティストを表示させる。

まず、表示するための元データ作り。
全日程を日付毎に管理する「NSDictornary」と各日付でのアーティストを管理す「NSArray」を3つ用意

NSDictionary *allDays;
NSArray *day1;
NSArray *day2;
NSArray *day3;

viewのloadと同時にデータを構築する

- (void)viewDidLoad {
    [super viewDidLoad];
	day1 = [NSArray arrayWithObjects:@"Cold Play",@"CSS",nil];
	day2 = [NSArray arrayWithObjects:@"faces",nil];
	day3 = [NSArray arrayWithObjects:@"cake",@"envy",nil];
	allDays = [NSDictionary dictionaryWithObjectsAndKeys:
			   day1,@"29日",
			   day2,@"30日",
			   day3,@"31日",nil];
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [allDays count];
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *days = [allDays allValues];
	
	return [[days objectAtIndex:section] count];
}

// ヘッダーにタイトルを付ける
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section{
	NSArray *daysTitle = [allDays allKeys];
	return [daysTitle objectAtIndex:section];
}

これで完成。最初のデータ構築の部分を動的に作成できたらいいのになーー。

おしまい。