2011-01-16 10 views
0

2つのセクションと1つのセルを持つUITableViewを持っています。最初のものをクリックすると最初のものをクリックした後、2番目のものが最初のコントローラに移動します。 RootViewControllerはnavigationControllerで、ViewControllerにプッシュしようとしています。私も悩みのセクションに複数のセクションと行/セルを追加することを抱えているUITableViewは1つのコントローラにプッシュする

// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if(section == 0) 
     return 1; 
    else 
     return 1; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
    if(section == 0){ 
     return @"Terminal/SSH Guides"; 
    }else{ 
     return @"Cydia Tutorials"; 
    } 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Set up the cell... 
    if(indexPath.section == 0){ 
     cell.text = @"Changing Password for root"; 
    } else { 
     cell.text = @"Hiding Sections"; 
    } 
    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    id newController; 
    switch (indexPath.row) { 
     case 0: 
      newController = [[rootpassword alloc] initWithNibName:@"rootpassword" bundle:nil]; 
      break; 
     case 1: 
      newController = [[hidingsections alloc] initWithNibName:@"hidingsections" bundle:nil]; 
      break; 
     default: 
      break; 
    } 
    [self.navigationController pushViewController:newController animated:TRUE]; 
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE]; 
} 

はここにtableViewのコードです。

ありがとうございました。

答えて

1

indexPath.srowの代わりに "didSelectRowAtIndexPath"のスイッチを実行する必要があると思います。なぜなら、両方のセクションには1つの行しかないからです。

+0

これはうまくいくようですが、それぞれにセクションと複数の行を追加したいのですが?ありがとう。 – Mitochondria

+0

あなたはあなたが持っているデータによってそれを行う多くの方法があります。通常は、セルを設定するためのデータを持つ配列foreachセクションのセクションにスイッチがあります。この例をチェックアウトhttp://www.mobisoftinfotech.com/blog/iphone/introduction-to-table-view/ – fsaint

+0

ああ、ありがとう。 if/switchループで動作するように管理されています。 – Mitochondria

1

各セクションには1つの行しかないので、indexPath.rowは常にゼロになります。 didSelectRowAtIndexPathでは、行ではなくセクションをテストする必要があります(セクションごとに1つの行のみを保持することを前提としています)。

関連する問題