Search code examples
uitableviewios5xcode4.2didselectrowatindexpath

pushViewController from cell at didSelectRowAtIndexPath to show different text content for each cell when pushed


i want to push a view controller for each cell selected, to show different text content for each cell when pushed

table A.h

#import <UIKit/UIKit.h>

@interface table_A : UITableViewController
{
    NSMutableArray *cars;
}

@end

I don't know if the method I'm using for didSelectRowAtIndexPath is correct.

table A.m

#import "table A.h"
#import "fordviewcontroller.h"

@implementation table_A

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

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{    
    [super viewDidLoad];

   cars=[[NSArray alloc]initWithObjects: @"ford",@"grand AM",@"nissan",nil];

    self.title = @"cars";


}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#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 [cars count];
}

- (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];
    }

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

    return cell;
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.

     fordviewcontroller *detailViewController = [[fordviewcontroller alloc] initWithNibName:@"fordviewcontroller" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];

}

@end

Solution

  • you should add a property to fordviewcontroller that represents the car. so in this case you'd have:

    @interface fordviewcontroller : UIViewController
    
    @property (copy) NSString *car;
    
    @end
    

    then in your table view controller:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      NSInteger row = indexPath.row;
      NSString *car = [cars objectAtIndex:row];
    
      fordviewcontroller *detailViewController = [[fordviewcontroller alloc] initWithNibName:@"fordviewcontroller" bundle:nil];
      detailViewController.car = car;
    
      [self.navigationController pushViewController:detailViewController animated:YES];
    }