2010-12-15 123 views
0

如何使单个单元格转到不同的视图控制器。现在,使用我使用的方法,如果我点击第一个单元格,它会带我到一个视图控制器,我有一个图片。现在,如果我返回并单击第二个单元格,它会将我带到相同的视图控制器。我如何改变这一点,以便所有其他单个单元格都转到单个视图控制器?我要添加什么或配置什么?如何将单个数据添加到单个单元格?

这里我的代码:

// 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:didSelectRowAtIndexPath方法:方法。您可以检查索引路径参数并执行不同的代码路径。例如:

-(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财产。你不会再需要它了。

三:实现的tableView:didSelectRowAtIndexPath方法:如下:

- (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]; 
} 
相关问题