2010-12-15 19 views
0

個々のセルを異なるView Controllerに移動するにはどうすればいいですか?今、私は最初のセルをクリックすると、私はピクチャを持っているビューコントローラに私をかかります使用してメソッドを使用して、使用しています。今すぐ戻って2番目のセルをクリックすると、同じビューコントローラーになります。他のすべてのセルが個々のView Controllerに移動するように、これをどのように変更するのですか?私は何を追加するのか、何を設定するのですか?ここ個々のデータを個々のセルに追加するにはどうすればよいですか?

私のコード:didSelectRowAtIndexPath::メソッド

// 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] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 

NSUInteger row = [indexPath row]; 
cell.textLabel.text = [breakdownArray objectAtIndex:row]; 

bookDetailViewController.title = [NSString stringWithFormat:@"%@" , [breakdownArray objectAtIndex:row]]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Navigation logic may go here. Create and push another view controller. 
/* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 
    */ 

NSInteger row = [indexPath row]; 
if (self.bookDetailViewController == nil); { 
    BookDetailViewController *aBookDetail = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:nil]; 
    self.bookDetailViewController = aBookDetail; 

    [aBookDetail release]; 

} 

bookDetailViewController.title = [NSString stringWithFormat:@"%@" , [breakdownArray objectAtIndex:row]]; 

Books2010AppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 
[delegate.breakdownNavController pushViewController:bookDetailViewController animated:YES]; 
} 

答えて

1

一つのかなり簡単な方法は、あなたのtableViewです。インデックスパスパラメータをチェックして、異なるコードパスを実行できます。ような何か:ほとんどのデザインの選択と同様に

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath)indexPath { 
    switch(indexPath.row) { 
     case 1: 
      [self.navigationController pushViewController:someViewController animated:YES]; 
      break; 
     case 2: 
      [self.navigationController pushViewController:someOtherViewController animated:YES]; 
      break; 
    } 
} 

が、これは一つの可能​​なアプローチのほんの一例です。それはこの一般的なケースで私ができることです。

0

同じビューを使用してプロパティを変更する代わりに、新しいビューコントローラを作成します。たとえば、tableView:didSelectRowAtIndexPath:でコメントアウトした例のように変更します。これは、これらの方法以外の場所からコントローラにアクセスしない場合にのみ機能します。

まず:あなたのtableViewからbookDetailViewController.title = [NSString stringWithFormat:@"%@" , [breakdownArray objectAtIndex:row]];を削除:cellForRowAtIndexPath:メソッド

セカンド(あなたはそれだけでより多くのプロセッサ時間を取っているので、とにかくこれを行う必要があります):あなたのクラスからbookDetailControllerプロパティを削除します。あなたはもうそれを必要としません。

サード:didSelectRowAtIndexPath::のtableViewを実装し、次のように:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path { 
    BookDetailViewController *bookDetailViewController = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:nil]; 
    NSInteger row = path.row; 
    bookDetailViewController.title = [NSString stringWithFormat:@"%@" , [breakdownArray objectAtIndex:row]]; 
    //Any other setup on view controller 
    Books2010AppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 
    [delegate.breakdownNavController pushViewController:bookDetailViewController animated:YES]; 
    [bookDetailViewController release]; 
} 
関連する問題