絶対に挫折しないiPhoneアプリ開発入門Part.13 ~Table View Controller その8、セクションを使いこなすその2、画面遷移~

前回:.絶対に挫折しないiPhoneアプリ開発入門Part.12 ~Table View Controller その7、セクションを使いこなすその1、セルの矢印を表示~

では、セクションをたくさん作った場合、cellForRowAtIndexPath で
どのようにセルの実装をしていくか見てみましょう。


if文を使ってセルが一個目を選択したときには。
さらにそのセクションのなかで画像を表示させるには。



BiyoushitsuViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    if (indexPath.section == 0) {
        cell.textLabel.text = [dataSource70_0_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [data70_0_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        if(indexPath.row == 0){
            cell.imageView.image = [UIImage imageNamed:@"ash.PNG"];
        }
        if(indexPath.row == 1){
            cell.imageView.image = [UIImage imageNamed:@"kera.PNG"];
        }
    }
    if (indexPath.section == 1) {
        cell.textLabel.text = [dataSource70_1_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [data70_1_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        if(indexPath.row == 0){
            cell.imageView.image = [UIImage imageNamed:@"waveup.png"];
        }   
    }
    if (indexPath.section == 2) {
        cell.textLabel.text = [dataSource70_2_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [data70_2_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        if(indexPath.row == 0){
            cell.imageView.image = [UIImage imageNamed:@"hair for man.PNG"];
        }
        if(indexPath.row == 1){
            cell.imageView.image = [UIImage imageNamed:@"plerna.png"];
        }
        if(indexPath.row == 2){
            cell.imageView.image = [UIImage imageNamed:@"merry land.PNG"];
        }
        if(indexPath.row == 3){
            cell.imageView.image = [UIImage imageNamed:@"Lapace.PNG"];
        }   
    }
    if (indexPath.section == 3) {
        cell.textLabel.text = [dataSource70_3_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [data70_3_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        if(indexPath.row == 0){
            cell.imageView.image = [UIImage imageNamed:@"act.png"];
        }
        if(indexPath.row == 1){
            cell.imageView.image = [UIImage imageNamed:@"yumeyui.png"];
        }
        if(indexPath.row == 2){
            cell.imageView.image = [UIImage imageNamed:@"earth.png"];
        }
        if(indexPath.row == 3){
            cell.imageView.image = [UIImage imageNamed:@"siletto.png"];
        }
        if(indexPath.row == 4){
            cell.imageView.image = [UIImage imageNamed:@"neolivejuq.png"];
        }
        if(indexPath.row == 5){
            cell.imageView.image = [UIImage imageNamed:@"beens.png"];
        }
        
    }
    if (indexPath.section == 4) {
        cell.textLabel.text = [dataSource70_4_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [data70_4_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        if(indexPath.row == 0){
            cell.imageView.image = [UIImage imageNamed:@"baron_hiyoshi.png"];
        }
        if(indexPath.row == 1){
            cell.imageView.image = [UIImage imageNamed:@"actj.png"];
        }
        if(indexPath.row == 2){
            cell.imageView.image = [UIImage imageNamed:@"chic.png"];
        }
        if(indexPath.row == 3){
            cell.imageView.image = [UIImage imageNamed:@"botanhair.png"];
        }
        
    }
    if (indexPath.section == 5) {
        cell.textLabel.text = [dataSource70_5_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [data70_5_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        if(indexPath.row == 0){
            cell.imageView.image = [UIImage imageNamed:@"TOKIO.png"];
        }
        if(indexPath.row == 1){
            cell.imageView.image = [UIImage imageNamed:@"watanabe_rihatsu.png"];
        }    
    } 
    if (indexPath.section == 6) {
        cell.textLabel.text = [dataSource70_6_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [data70_6_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        if(indexPath.row == 0){
            cell.imageView.image = [UIImage imageNamed:@"baronsun.png"];
        }
    }
    if (indexPath.section == 7) {
        cell.textLabel.text = [dataSource70_7_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [data70_7_ objectAtIndex:indexPath.row];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        if(indexPath.row == 0){
            cell.imageView.image = [UIImage imageNamed:@"loops.png"];
        }
    }
    
    return cell;
}


Part.1からこのブログを読んでくれた方にはもう特にこの部分に関しては言う必要はないかなと思います。


こうやってif文で画像をひとつひとつコードで書いて実装するのはダサい気もしますが、

きちんと表示されるはずです。





では次に、セクションからの

画面遷移

を実装してみましょう。


まずは、画面遷移後に使うviewを新規作成して、

BiyoushitsuViewController.mファイルにインポートしますよ〜


BiyoushitsuViewController.m

#import "BiyoushitsuViewController.h"
#import "AshHiyoshi.h"
#import "Kera.h"
#import "WaveUp.h"
#import "ForMen.h"
#import "HairPlerna.h"
#import "MerryLand.h"
#import "LaPace.h"
#import "ActJj.h"
#import "Actj.h"
#import "YumeYui.h"
#import "HairEarth.h"
#import "Siletto.h"
#import "NeoLive.h"
#import "Beens.h"
#import "BaronDuo.h"
#import "Chic.h"
#import "Botan.h"
#import "Tokio.h"
#import "WatanabeRihatsu.h"
#import "Baron2.h"
#import "Loops.h"

そして、
if文の中にif文をいれるという「入れ子構造」で実装します。

BiyoushitsuViewController.m

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            AshHiyoshi *hh = [[AshHiyoshi alloc] initWithNibName:@"AshHiyoshi" bundle:nil];
            [self.navigationController pushViewController:hh animated:YES];
            [hh release];
        }
        if (indexPath.row == 1) {
            Kera *ty = [[Kera alloc] initWithNibName:@"Kera" bundle:nil];
            [self.navigationController pushViewController:ty animated:YES];
            [ty release];
        }
        
        
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            WaveUp *bn = [[WaveUp alloc] initWithNibName:@"WaveUp" bundle:nil];
            [self.navigationController pushViewController:bn animated:YES];
            [bn release];
        }
        
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            ForMen *mr = [[ForMen alloc] initWithNibName:@"ForMen" bundle:nil];
            [self.navigationController pushViewController:mr animated:YES];
            [mr release];
        }
        if (indexPath.row == 1) {
            HairPlerna *ww = [[HairPlerna alloc] initWithNibName:@"HairPlerna" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        if (indexPath.row == 2) {
            MerryLand *ww = [[MerryLand alloc] initWithNibName:@"MerryLand" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        if (indexPath.row == 3) {
            LaPace *ww = [[LaPace alloc] initWithNibName:@"Lapace" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        
    }
    if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            Actj *pt = [[Actj alloc] initWithNibName:@"Actj" bundle:nil];
            [self.navigationController pushViewController:pt animated:YES];
            [pt release];
        }
        if (indexPath.row == 1) {
            YumeYui *ww = [[YumeYui alloc] initWithNibName:@"YumeYui" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        if (indexPath.row == 2) {
            HairEarth *ww = [[HairEarth alloc] initWithNibName:@"HairEarth" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        if (indexPath.row == 3) {
            Siletto *ww = [[Siletto alloc] initWithNibName:@"Siletto" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        if (indexPath.row == 4) {
            NeoLive *ww = [[NeoLive alloc] initWithNibName:@"NeoLive" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        if (indexPath.row == 5) {
            Beens *ww = [[Beens alloc] initWithNibName:@"Beens" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        
        
    }
    if (indexPath.section == 4) {
        if (indexPath.row == 0) {
            BaronDuo *ut = [[BaronDuo alloc] initWithNibName:@"BaronDuo" bundle:nil];
            [self.navigationController pushViewController:ut animated:YES];
            [ut release];
        }
        if (indexPath.row == 1) {
            ActJj *ww = [[ActJj alloc] initWithNibName:@"ActJj" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        if (indexPath.row == 2) {
            Chic *ww = [[Chic alloc] initWithNibName:@"Chic" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        if (indexPath.row == 3) {
            Botan *ww = [[Botan alloc] initWithNibName:@"Botan" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        
        
        
    }
    if (indexPath.section == 5) {
        if (indexPath.row == 0) {
            Tokio *hc = [[Tokio alloc] initWithNibName:@"Tokio" bundle:nil];
            [self.navigationController pushViewController:hc animated:YES];
            [hc release];
        }
        if (indexPath.row == 1) {
            WatanabeRihatsu *ww = [[WatanabeRihatsu alloc] initWithNibName:@"WatanabeRihatsu" bundle:nil];
            [self.navigationController pushViewController:ww animated:YES];
            [ww release];
        }
        
        
    }
    if (indexPath.section == 6) {
        if (indexPath.row == 0) {
            Baron2 *hc = [[Baron2 alloc] initWithNibName:@"Baron2" bundle:nil];
            [self.navigationController pushViewController:hc animated:YES];
            [hc release];
        }
        
    }
    if (indexPath.section == 7) {
        if (indexPath.row == 0) {
            Loops *hc = [[Loops alloc] initWithNibName:@"Loops" bundle:nil];
            [self.navigationController pushViewController:hc animated:YES];
            [hc release];
        }
        
    }
}

@end



セクション(section)のなかの列(row)ですね。



はっきり言いまして、ものすごいファイル数になります。

ご自分で試されるときは、適当に減らして、あるいは自分が作りたいアプリの都合に合わせて、
セクションを使ってみてくださいな^^




次回:絶対に挫折しないiPhoneアプリ開発入門Part.14 ~UIWebView、webページを表示する~


======================
当ブログ管理人のツイッターこちら
◎フォローしてくださると泣いて喜びます!ツイッター上で当ブログの質問などにもできるだけお答えしますし、役に立つiPhoneアプリ開発情報もつぶやきます。個人的なご依頼(たとえば、プログラミングの家庭教師、Skypeレッスンをしてくれないか、iPhoneアプリ開発の勉強会ってやってるの、とかですね)でもかまいません。
スカイプレッスンについて
開発をしているとその都度ぶつかる問題があると思いますが、検索で調べてもなかなかわからない、あるいは調べても非常に時間がかかる場合があると思います。開発者のみなさんもお忙しいと思いますので、私のレッスンを受けながら開発を進めていただけると、大幅にお時間を短縮できます。加えて、iPhoneアプリ開発の基礎、応用が身につき、今後の開発もスムーズに進められます

お問い合わせはツイッター、またはhiyoshimarukoアットマークgmail.comまでお願いします。(アットマークを@にしてください)お待ちしております!




【絶対に挫折しないiPhoneアプリ開発入門】
番外編 絶対に挫折しないiPhoneアプリ開発入門 番外編1 プログラミング初心者が初めてアプリ開発に挑戦する時
1.絶対に挫折しないiPhoneアプリ開発入門Part.1 ~Xcode4.2でひよしまるを作ろう~
2.絶対に挫折しないiPhoneアプリ開発入門Part.2 ~iOS Developer Programに参加しよう~
3.絶対に挫折しないiPhoneアプリ開発入門Part.3 ~Xcode4.2の起動、InterfaceBuilderの利点欠点、神本紹介~
4.絶対に挫折しないiPhoneアプリ開発入門Part.4 ~Single ViewControllerからスタート~
5.絶対に挫折しないiPhoneアプリ開発入門Part.5 ~画面遷移、Tab Bar Controller,Navigation Controller,Table View Controller~
6.絶対に挫折しないiPhoneアプリ開発入門Part.6 ~UITableViewController その1、Tabの名前変更~
7.絶対に挫折しないiPhoneアプリ開発入門Part.7~Table View Controller その2(ファイルの接続)、Objective C、コメントについて~
8.絶対に挫折しないiPhoneアプリ開発入門Part.8 ~Table View Controller その3、ナビゲーションバーにタイトルと色をつける~
9.絶対に挫折しないiPhoneアプリ開発入門Part.9 ~Table View Controller その4、numberOfSectionsInTableView、numberOfRowsInSection、Xcodeの背景を黒に~
10.絶対に挫折しないiPhoneアプリ開発入門Part.10 ~Table View Controller その5、cellForRowAtIndexPath~
11.絶対に挫折しないiPhoneアプリ開発入門Part.11 ~Table View Controller その6、画面遷移、didSelectRowAtIndexPath、タブバーを消す、アニメーション~
12.絶対に挫折しないiPhoneアプリ開発入門Part.12 ~Table View Controller その7、セクションを使いこなすその1、セルの矢印を表示~
13.絶対に挫折しないiPhoneアプリ開発入門Part.13 ~Table View Controller その8、セクションを使いこなすその2、画面遷移~
14.絶対に挫折しないiPhoneアプリ開発入門Part.14 ~UIWebView、webページを表示する~
15.絶対に挫折しないiPhoneアプリ開発入門Part.15 ~UITextView、電話とリンクをText Viewで表示~
16.絶対に挫折しないiPhoneアプリ開発入門Part.16 ~Table View Controller その9、テーブルを画面遷移させない、App Store申請Reject~