Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My question is ay when the user presses "Tennessee Hops", when they press that I want to it to push to a detail view listing the specs of that area... So from the opening of the app it would go Tennessee(RootTable) -> Tennessee Hops(SecondTable) -> Specs(DetailView). Can someone lead me in the right direction as to how to get this to function correctly?

Thanks!

RootTableViewController.h
Objective-C
#import <UIKit/UIKit.h>

@interface RootTableViewController : UITableViewController

@end


RootTableViewController.m
Objective-C
#import "RootTableViewController.h"
#import "SecondTableViewController.h"

@interface RootTableViewController ()

@end

@implementation RootTableViewController
{
    NSMutableArray *states;
}

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    states = [NSMutableArray arrayWithObjects:@"Alabama", @"Georgia", @"Tennessee", nil];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [states count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"StatesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    cell.textLabel.text = [states objectAtIndex:indexPath.row];
    return cell;
}
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"showArrayDetail"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        SecondTableViewController *destViewController = segue.destinationViewController;
        destViewController.stateName = [states objectAtIndex:indexPath.row];
        destViewController.title = destViewController.stateName;
    }
}


@end


SecondTableViewController.h
Objective-C
#import <UIKit/UIKit.h>

@interface SecondTableViewController : UITableViewController

@property (nonatomic, strong) NSString *stateName;

@property (nonatomic, strong) NSString *areaName;

@end


SecondTableViewController.m
Objective-C
#import "SecondTableViewController.h"

@interface SecondTableViewController ()

@end

@implementation SecondTableViewController
{
    NSMutableArray *alabama;
    NSMutableArray *georgia;
    NSMutableArray *tennessee;
}

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    alabama = [NSMutableArray arrayWithObjects:@"Moss Rock Preserve Boulder Fields", nil];
    georgia = [NSMutableArray arrayWithObjects:@"Georgia Pines", nil];
    tennessee = [NSMutableArray arrayWithObjects:@"Tennessee Hops", nil];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if([_stateName isEqualToString:@"Alabama"])
    {
        return [alabama count];
    }
    
    else if([_stateName isEqualToString:@"Georgia"])
    {
        return [georgia count];
    }
    
    else if([_stateName isEqualToString:@"Tennessee"])
    {
        return [tennessee count];
    }
    
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"Animal2Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    if([_stateName isEqualToString:@"Alabama"])
    {
        cell.textLabel.text = [alabama objectAtIndex:indexPath.row];
    }
    
    else if([_stateName isEqualToString:@"Georgia"])
    {
        cell.textLabel.text = [georgia objectAtIndex:indexPath.row];
    }
    
    else if([_stateName isEqualToString:@"Tennessee"])
    {
        cell.textLabel.text = [tennessee objectAtIndex:indexPath.row];
    }
    
    return cell;
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900