0

Does anyone know of any tutorials out there that explains how to push to new details views. Right now I have 2 TableViews, the first TableView holds states and the second TableView holds area names. From the second TableView I want to be able to press an area and display information in the DetailView, but I'm not sure as to how to get that information to the DetailView.

RootTableViewController.h

#import <UIKit/UIKit.h>

@interface RootTableViewController : UITableViewController

@end

RootTableViewController.m

#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;
}

SecondTableViewController.h

#import <UIKit/UIKit.h>

@interface SecondTableViewController : UITableViewController
{
NSMutableArray *listOfStates;
}
@property (retain, atomic) NSMutableArray *listOfStates;
@property (nonatomic, strong) NSString *stateName;

@end

SecondTableViewController.m

#import "SecondTableViewController.h"
#import "RootTableViewController.h"

@interface SecondTableViewController ()

@end

@implementation SecondTableViewController

@synthesize listOfStates = _listOfStates;

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

- (void)viewDidLoad
{
[super viewDidLoad];

_listOfStates = [NSDictionary dictionary];


_listOfStates  =
@{
@"Alabama":@[
        @{@"area":@"albama area 1", @"description":@"Alabama specs 1"},
        @{@"area":@"albama area 2", @"description":@"Alabama specs 2"},
        @{@"area":@"albama area 3", @"description":@"Alabama specs 3"},
        ],
@"Georgia":@[
        @{@"area":@"Georgia area 1", @"description":@"Georgia specs 1"},
        @{@"area":@"Georgia area 2", @"description":@"Georgia specs 2"},
        @{@"area":@"Georgia area 3", @"description":@"Georgia specs 3"}
        ],
@"Tennessee":@[
        @{@"area":@"Tennessee area 1", @"description":@"Tennessee specs 1"},
        @{@"area":@"Tennessee area 2", @"description":@"Tennessee specs 2"},
        @{@"area":@"Tennessee area 3", @"description":@"Tennessee specs 3"}
        ]

};

NSArray *state = [_listOfStates objectForKey:_stateName];

// 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 [self.state count];
}


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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.textLabel.text = [[self.state objectAtIndex:indexPath.row] objectForkey:@"area"];

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"detailSegue" sender:sender];
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"detailSegue"])
{
    //get the specs
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSString *specs = [_informationSpecs objectAtIndex:indexPath.row];

    //set the specs
    DetailViewController *detailVC= (DetailViewController *)segue.destinationViewController;
    detailVC.description= [[self.state objectAtIndex:indexPath.row] objectForkey:@"description"];
}
}

@end

Errors

SecondTableViewController.m:43:10: Expected ':'

SecondTableViewController.m:58:37: No visible @interface for 'NSDictionary' declares the selector 'obejectForKey:'

SecondTableViewController.m:67:9: Use of undeclared identifier 'didReceiveMemoryWarning'

Thanks for any guidance.

javaGeek
  • 304
  • 1
  • 4
  • 11
  • See [this answer][1], I think this is you are looking for. [1]: http://stackoverflow.com/questions/12464164/pushing-to-a-detail-view-from-a-table-view-cell-using-xcode-storyboard – Jan Cássio Apr 05 '14 at 20:33

2 Answers2

1

Instead of having array for each state you should put data together because they are related.

List of States:

_listOfStates  =
@{
  @"Alabama":@[
            @{@"area":@"albama area 1", @"description":@"Alabama specs 1"},
            @{@"area":@"albama area 2", @"description":@"Alabama specs 2"},
            @{@"area":@"albama area 3", @"description":@"Alabama specs 3"},
            ],
  @"Georgia":@[
            @{@"area":@"Georgia area 1", @"description":@"Georgia specs 1"},
            @{@"area":@"Georgia area 2", @"description":@"Georgia specs 2"},
            @{@"area":@"Georgia area 3", @"description":@"Georgia specs 3"}
            ],
  @"Tennessee":@[
            @{@"area":@"Tennessee area 1", @"description":@"Tennessee specs 1"},
            @{@"area":@"Tennessee area 2", @"description":@"Tennessee specs 2"},
            @{@"area":@"Tennessee area 3", @"description":@"Tennessee specs 3"}
            ]

  };

State Array:

self.state = [_listOfStates objectForKey:_stateName];

numberOfRowsInSection

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

cellForRowAtIndexPath

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    cell.textLabel.text = [[self.state objectAtIndex:indexPath.row] objectForkey:@"area"];

    return cell;
}

didSelectRowAtIndexPath

 -(void)tableView:(UITableView *)tableView 
                            didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   [self performSegueWithIdentifier:@"detailSegue" sender:sender];
 }

prepareForSegue

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"detailSegue"])
    {  
       //get the specs
       NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
       NSString *description = [[self.state objectAtIndex:indexPath.row] objectForkey:@"description"];

       //set the specs
       DetailViewController *detailVC= (DetailViewController *)segue.destinationViewController;
       detailVC.description= description;
    }
}

DEMO PROJECT

meda
  • 45,103
  • 14
  • 92
  • 122
  • What I am most confused about is where do I add the area specs in my program? under what method and how will that alter my "cellForRowAtIndexPath", "numberOfRowsInSection" and "numberOfSectionsInTableView" since I will be adding more arrays.... – javaGeek Apr 05 '14 at 21:42
  • You need to have data related together that's your issue something like `_listOfSpecs = @[ @{@"name" :@"Alabama", @"specs":@"Alabama specs"}, @{@"name" :@"Georgia", @"value":@"Georgia specs"}, @{@"name" :@"Tennessee", @"value":@"Tennessee specs"}, ];` – meda Apr 05 '14 at 22:11
  • Could you try and implement into the answer you have above? It is kind of hard for me to follow that. Sorry, I am really new to this, I know this is probably very simple. – javaGeek Apr 05 '14 at 22:19
  • What are the minor problems? Why is it saying they are undeclared, when they are declared? – javaGeek Apr 06 '14 at 02:00
  • I just updated it to the correct code and still have 3 existing errors, I updated my errors above. Got rid of 6 though – javaGeek Apr 06 '14 at 02:10
  • 1
    @javaGeek soory that was a typo `objectForKey` where is the line for the first error and last – meda Apr 06 '14 at 02:15
  • line for 1st error is the closed bracket and comma right above "@{@"Georgia":@[" in listOfStates. line for 2nd error is "- (void)didReceiveMemoryWarning" – javaGeek Apr 06 '14 at 02:21
  • @javaGeek delete `didReceiveMemoryWarning` and check my new listOfarray – meda Apr 06 '14 at 02:29
  • It looks like i am missing a bracket to close my viewDidLoad, but when I put it in there, 9 errors pop back up.... – javaGeek Apr 06 '14 at 02:36
  • just added update version of that method – javaGeek Apr 06 '14 at 02:38
0

Implement a method in your detail view, that receives the changed data and updates local variables, if any, accordingly and updates the view. Call that method from your master view controller.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71