0

我是 iPhone 开发的新手,正在尝试创建一个具有多个视图的小型应用程序(配方列表)。当用户选择 tablecell 中的某一行时,我想调用一个新视图。例如,带有蘑菇、意大利面、牛排的菜单卡必须调用具有详细食谱的另一个视图。

我使用 UITableViewController 来显示带有附件的项目列表。这些项目是从 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

// 食谱.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
4

1 回答 1

0

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

我们基于 a 的选择向用户显示不同视图控制器的应用程序的标准方法UITableViewCell是使用UINavigatorController. 该控制器为您提供了以下方法:

  • – pushViewController:动画:
  • – popViewControllerAnimated:

……等等。使用这两种方法,您可以在视图控制器之间来回切换,这正是您想要做的。所以你必须给你的导航器控制器你已经拥有的表格视图控制器。您可以使用UINavigatorController's 方法来做到这一点– 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];
}

希望这可以帮助!

进一步阅读:

于 2013-09-09T21:47:48.477 回答