如何根据Tableviewcell中的RowSelected显示不同的视图控制器?

时间:2013-09-09 21:34:48

标签: ios uitableview

我是iPhone开发的新手,并尝试创建一个包含多个视图的小应用程序(配方列表)。当用户选择tablecell中的一行时,我想调用一个新视图。对于Instance,带有蘑菇,意大利面,牛排的菜单卡必须使用详细的配方调用另一个视图。

我使用UITableViewController显示带有Accessory的项目列表。这些项目是从NSArray动态填充的。当用户点击表中的一行时,应该根据行选择将他带到视图控制器。我知道我必须实现tableView:didSelectRowAtIndexPath:函数来调用相应的视图。 但我无法继续。我在故事板中读到了Segue,但即使这对我来说也有点混乱。

如果有人解决我的疑问,我真的很感激。这是我的代码。

RecipeViewController.h

    #import "RecipeViewController.h"
    #import "Recipe.h"

    @interface RecipeViewController ()

    @end

@implementation RecipeViewController

{
    NSArray *recipes; //Recipe Array
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.title = @"Hotel";
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
    [[self navigationItem] setBackBarButtonItem:backButton];

    // Create recipe array for main screen. 

    Recipe *recipe1 = [Recipe new];
    recipe1.headline = @"Mushroom";
    recipe1.description = @"description";
    recipe1.imageFile = @"mushroom.jpg";

    Recipe *recipe2 = [Recipe new];
    recipe2.headline = @"pasta";
    recipe2.description = @"Pasta description";
    recipe2.imageFile = @"pasta.jpg";

    Recipe *recipe3 = [Recipe new];
    recipe3.headline = @"Steak";
    recipe3.description = @"Steak description";
    recipe3.imageFile = @"steak.jpg";


    recipes = [NSArray arrayWithObjects:recipe1, recipe2, recipe3,nil];

    // Remove table cell separator
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    // Assign custom backgroud for the view
    self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"welcome_screen"]];
    self.tableView.backgroundColor = [UIColor clearColor];

    // Add padding to the top of the table view
    UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
    self.tableView.contentInset = inset;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return recipes.count;
}

- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
    NSInteger rowIndex = indexPath.row;
    UIImage *background = nil;

    if (rowIndex == 0) {
        background = [UIImage imageNamed:@"cell_top.png"];
    } else if (rowIndex == rowCount - 1) {
        background = [UIImage imageNamed:@"cell_bottom.png"];
    } else {
        background = [UIImage imageNamed:@"cell_middle.png"];
    }

    return background;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Display recipe in the table cell
    Recipe *recipe = [recipes objectAtIndex:indexPath.row];
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:recipe.imageFile];

    UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
    recipeNameLabel.text = recipe.headline;

    UILabel *recipeDetailLabel = (UILabel *)[cell viewWithTag:102];
    recipeDetailLabel.text = recipe.description;

    // Assign our own background image for the cell
    UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];

    UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
    cellBackgroundView.image = background;
    cell.backgroundView = cellBackgroundView;


    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    CampusViewController *campusViewController;
WhoViewController *whoViewController;



    switch (indexPath.row) 
 {
         case 0:
          campusViewController=[[CampusViewController alloc] init];  
             [[self navigationController] pushViewController:campusViewController      animated:YES];

        break;

    case 1:
        whoViewController=[[WhoViewController alloc] init];   // not released as ARC is   included in the project
        [[self navigationController] pushViewController:whoViewController animated:YES];
        break;
    case 2:

        break;
    case 3:

        break;
    default:
        break;
}

}

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
  RecipeViewController *detailViewController = [segue destinationViewController];
  NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];

    /* NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
 detailViewController.data = [self.dataController objectInListAtIndex:indexPath.row]; 

    */

      }



   }

    @end

// Recipe.h

#import <Foundation/Foundation.h>

@interface Recipe : NSObject

@property (nonatomic, strong) NSString *headline; // name of recipe
@property (nonatomic, strong) NSString *description; // recipe detail
@property (nonatomic, strong) NSString *imageFile; // image filename of recipe

@end

1 个答案:

答案 0 :(得分:0)

您是否阅读过有关如何执行此操作的任何教程?这是一个非常基本的问题,有很多关于此的帖子。 我只会给你一个如何做的解释,但你必须自己编写代码。

我们根据UITableViewCell的选择向应用程序显示用户不同视图控制器的标准方法是使用UINavigatorController。该控制器为您提供方法:

  • - pushViewController:animated:
  • - popViewControllerAnimated:

......等等。使用这两种方法,您可以在视图控制器之间来回切换,这是您想要做的。因此,您必须为导航器控制器提供您已有的表视图控制器。您可以使用UINavigatorController的方法– initWithRootViewController:

来实现

– tableView:willSelectRowAtIndexPath:可能是什么样子的一个小例子:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *myViewController = [[UIViewController alloc] initWithNibName:@"YourNibName" bundle:nil];
    self.navigationController pushViewController:[myViewController autorelease] animated:YES];
}

希望这有帮助!

进一步阅读: