2011-07-22 9 views
-1

だから、私は3セクションを持っています。押すビューコントローラが

これは私が使用しているコードです:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    switch (indexPath.row) 
    { 
     case 0: 
      [self.navigationController pushViewController:[[[FirstViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES]; 
      break; 

     case 1: 

      [self.navigationController pushViewController:[[[SecondViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES]; 
      break; 
    etc..  
} 
} 

これは私の問題である: "A" に私は6つの要素を持っています。 0から5までのスイッチを使用すると、正しいView Controllerを押し出すことができます。しかし、私は "B"セクションのためのView Controllerを押し出す必要があります、それは別の9つの要素を含んでいます、私は先に進みます(そうケース7,8など)、 。?。再びFirstViewController」。など)のために同じ話 "C" セクションこれを解決する方法

私はいまいましい、グループ化されたテーブルを嫌うんだ

編集押し出す:新しいコード、ループ

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    switch (indexPath.section) 
    { 
     case 0: 


      switch (indexPath.row) { 
       case 0: 

        [self.navigationController pushViewController:[[[FirstViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES]; 
       break; 

       case 1: 

        [self.navigationController pushViewController:[[[SecondViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES]; 
       break; 
     } 

     case 1: 
      switch (indexPath.row) { 
       case 0: 
      [self.navigationController pushViewController:[[[ThirdViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES]; 
      break; 
     case 1: 
      [self.navigationController pushViewController:[[[FourthViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES]; 
      break; 

     case 2: 
      [self.navigationController pushViewController:[[[FifthViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES]; 
      break; 

      } 
     case 2: 
      switch (indexPath.row) { 
       case 0: 
      [self.navigationController pushViewController:[[[SixthViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES]; 
      break; 

     case 1: 
      [self.navigationController pushViewController:[[[SeventhViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES]; 
      break; 



      } 
    } 
} 

私は明らかに長すぎるそうでない場合は、コードをカットしました

編集2:

Worked!セルジオの答えは正しかったが、私はswitch (indexPath.section){}

+0

これは 'indexPath.row'があるところで' indexPath.section'を使うべきように読んでいます。 – PengOne

答えて

3

渡されdidSelectRowAtIndexPathは二つの性質があることNSIndexPathindexPath引数の代わりにif (indexPath.section == 0)を置く:rowsectionを。 section情報を正しく使用すると、3つのセクションを区別することができます。

など。 (これは醜いですが、それは動作します):あなたは大きなスイッチを必要としないように

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    switch (indexPath.section) 
    { 
     case 0: 
     switch (indexPath.row) 
     { 
      case 0: 
      ... 
      break; 
      ... 
     } 
     break; 

     case 1: 

     switch (indexPath.row) 
     { 
      case 0: 
      ... 
      break; 
      ... 
     } 
     break; 

     etc... 
    } 
} 

よりよい解決策は、IMO、データソースの各要素に関連するサブテーブルタイプでエンコードされます各セルはすでにどのような種類のサブテーブルを開くべきかを「知っている」。 Classタイプ(this questionもご覧ください)を調べることができます。

+0

最初の方法が機能します。私が得た問題は、ビューを押し出すと、セクションに戻る必要があるときに、View Controllerがナビゲーションに入る前に2回戻ります。 :Sこの問題は、最初の10個の要素にしかありません – Phillip

+1

「View Controllerはナビゲーションに入る前に2回自分自身に戻ります」とはどういう意味ですか?あなたはそれを2回プッシュしましたか?私はあなたに示したように、これが 'indexPath.section'の使用に関連しているとは思わない... – sergio

+0

コントローラを押し出すと、左上のナビゲーションに戻ることができる戻るボタンがあります?私がそれを押すと、セクションナビゲータに行く前に同じページに戻ります。それはちょっと自分自身を2回ループします。私はそれを2回押し込んだとは思わない。私は自分のコードを投稿するつもりです。 – Phillip

関連する問題