テーブルのデリゲート

セクションの数を返すメソッド

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;     // セクション数
}

行数を返すメソッド

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // セクションによって分岐
    if( section == 0 ){
        return myData1.count;     // 行数
    }else{
        return myData2.count;     // 行数
    }
}

ヘッダーに表示する文字を返すメソッド

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // セクションによって分岐
    if( section == 0 ){
        return @"ヘッダー1";
    }else{
        return @"ヘッダー2";
    }
}

テーブルに表示するセルを返すメソッド

- (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.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    // セクションによって分岐
    if( indexPath.section == 0 ){
       // myData1の配列番号とindexPath.row(何番目のセルかを示す)の対応で取り出す
        cell.textLabel.text = [myData1 objectAtIndex:indexPath.row];
    }else{
       // myData2の配列番号とindexPath.row(何番目のセルかを示す)の対応で取り出す
        cell.textLabel.text = [myData2 objectAtIndex:indexPath.row];
    }
    
    return cell;
}

どういうときにデリゲートしてどういうときにデリゲートしないのか

テーブルを作る場合、インタフェースビルダーでTable Viewをドラッグアンドドロップする方法

New ProjectからMaster-Detail Applicationを選んで開発するパターン
の2つになると思われるが、
前者のTable Viewをドラッグアンドドロップする場合は、デリゲートが必要で、
後者のMaster-Detail Applicationを使う場合は、Master-Detail Applicationが用意したメソッドを使えば
自動でデリゲートしているみたいなので、自分でデリゲートする場面がいらない場合がある。


記憶方法としては、
インタフェースビルダーでドラッグアンドドロップしたときは、
自分でデリゲートしろ。