I am trying to push data in an array to a new UITableView
. I have completed the code and there are no errors, but it is pushing to a blank view (not pushing array data to new view).
When the user presses "Alabama Area 1" it should pull up the 3 climbs in that area that are in the alabamaArea1 array and the same thing for "Alabama Area 2". It is not pushing the data though. I will post my code below.
RootTableViewController.h
:
#import <UIKit/UIKit.h>
@interface RootTableViewController : UITableViewController
@end
RootTableViewController.m
:
#import "RootTableViewController.h"
#import "SecondTableViewController.h"
@interface RootTableViewController ()
@end
@implementation RootTableViewController
{
NSArray *states;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
states = [NSArray arrayWithObjects:@"Alabama", @"Georgia", @"Tennessee", @"Colorado", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#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
{
//table identifier
static NSString *simpleTableIdentifier = @"StateCell";
//creating a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
//if cell doesn't have anything in it, creates a new one
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
//creates text for cell, depending on what row it is
cell.textLabel.text = [states objectAtIndex:indexPath.row];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//push segue identifier 'showArrayDetail'
if([segue.identifier isEqualToString:@"showStateDetail"])
{
//row that we clicked on
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//'SecondTableVieController' object is created
SecondTableViewController *destViewController = segue.destinationViewController;
//sets 'stateName' to what row you pick
destViewController.stateName = [states objectAtIndex:indexPath.row];
//sets title to 'stateName' you picked
destViewController.title = [NSString stringWithFormat:@"%@ Areas", destViewController.stateName];
}
}
@end
SecondTableViewController.h
:
#import <UIKit/UIKit.h>
@interface SecondTableViewController : UITableViewController
@property (nonatomic, strong) NSString *stateName;
@end
SecondTableViewController.m
:
#import "SecondTableViewController.h"
#import "ThirdTableViewController.h"
@interface SecondTableViewController ()
@end
@implementation SecondTableViewController
{
NSArray *alabama;
NSArray *georgia;
NSArray *tenesssee;
NSArray *colorado;
NSArray *areas;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//populating arrays
alabama = [NSArray arrayWithObjects:@"Alabama Area 1", @"Alabama Area 2", @"Alabama Area 3", nil];
georgia = [NSArray arrayWithObjects:@"Georgia Area 1", @"Georgia Area 2", @"Georgia Area 3", nil];
tenesssee = [NSArray arrayWithObjects:@"Tennessee Area 1", @"Tennessee Area 2", @"Tennessee Area 3", nil];
colorado = [NSArray arrayWithObjects:@"Colorado Area 1", @"Colorado Area 2", @"Colorado Area 3", nil];
}
- (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
{
//returns number of rows dependent on which state was selected
if([_stateName isEqualToString:@"Alabama"])
{
return [alabama count];
}
else if([_stateName isEqualToString:@"Georgia"])
{
return [georgia count];
}
else if([_stateName isEqualToString:@"Tennessee"])
{
return [tenesssee count];
}
else if([_stateName isEqualToString:@"Colorado"])
{
return [colorado count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//table identifier
static NSString *simpleTableIdentifier = @"AreaCell";
//creating a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
//if cell doesn't have anything in it, creates a new one
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
//depending on what string equals depends on what array values are displayed
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 = [tenesssee objectAtIndex:indexPath.row];
}
else if([_stateName isEqualToString:@"Colorado"])
{
cell.textLabel.text = [colorado objectAtIndex:indexPath.row];
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//push segue identifier 'showArrayDetail'
if([segue.identifier isEqualToString:@"showAreaDetail"])
{
//row that we clicked on
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//'SecondTableVieController' object is created
ThirdTableViewController *destViewController = segue.destinationViewController;
//sets 'areaName' to what row you pick
destViewController.areaName = [areas objectAtIndex:indexPath.row];
//sets title to 'areaName' you picked
destViewController.title = [NSString stringWithFormat:@"%@ Climbs", destViewController.areaName];
}
}
@end
ThirdTableViewController.h
:
#import <UIKit/UIKit.h>
@interface ThirdTableViewController : UITableViewController
@property (nonatomic, strong) NSString *areaName;
@end
ThirdTableViewController.m
:
#import "ThirdTableViewController.h"
@interface ThirdTableViewController ()
@end
@implementation ThirdTableViewController
{
NSArray *alabamaArea1;
NSArray *alabamaArea2;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//populating arrays
alabamaArea1 = [NSArray arrayWithObjects:@"Alabama Climb 1", @"Alabama Climb 2", @"Alabama Climb 3", nil];
alabamaArea2 = [NSArray arrayWithObjects:@"Alabama Climb 4", @"Alabama Climb 5", @"Alabama Climb 6", 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
{
//returns number of rows dependent on which state was selected
if([_areaName isEqualToString:@"Alabama Area 1"])
{
return [alabamaArea1 count];
}
else if([_areaName isEqualToString:@"Alabama Area 2"])
{
return [alabamaArea2 count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//table identifier
static NSString *simpleTableIdentifier = @"ClimbCell";
//creating a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
//if cell doesn't have anything in it, creates a new one
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
//depending on what string equals depends on what array values are displayed
if([_areaName isEqualToString:@"Alabama Area 1"])
{
cell.textLabel.text = [alabamaArea1 objectAtIndex:indexPath.row];
}
else if([_areaName isEqualToString:@"Alabama Area 2"])
{
cell.textLabel.text = [alabamaArea2 objectAtIndex:indexPath.row];
}
return cell;
}
I have tested my code out and the problem seems to be coming from the cellForRowAtIndexPath
method. I am not looking for someone to hold my hand and tell me the answer, just some proper guidance and contrastive criticism.
Thanks