I will preface the question with I am new to iOS and could use a little push. I have been trying to figure this out for a few days and fear I am not able to figure out the solution out of my frustration. It is my hope that some new eyes backed with experience will be able to help me out with this.
I have a JSON file that I want to use for various portions of my application. The file can be viewed at https://raw.github.com/irong8/stronger-nation-data/master/data.json for reference.
I am using Storyboards and want to accomplish this using the built in JSON support of iOS5. I created a new TableViewController subclass and have included the code below.
Here is my .h file
#import <UIKit/UIKit.h>
@interface StateTableViewController : UITableViewController
{
NSArray *StateList;
}
@property (nonatomic, retain) NSArray *StateList;
- (void) buildStateList;
@end
Here is my .m file
#import "StateTableViewController.h"
@implementation StateTableViewController
@synthesize StateList;
- (void)buildStateList {
NSString *jsonFile = [ [NSBundle mainBundle] pathForResource:@"data" ofType:@"json" ];
NSError *jsonError = nil;
NSData *jsonData = [NSData dataWithContentsOfFile:jsonFile options:kNilOptions error:&jsonError ];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];
NSArray *jsonArray = [json objectForKey:@"states"];
self.StateList = jsonArray;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self buildStateList];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [StateList 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 = [NSString stringWithFormat:@"%d", indexPath.row];
return cell;
}
I can loop through the StateList array using the following and see the state names I am looking for.
for (NSString *element in StateList) {
NSLog(@"element: %@", element);
}
When I load this view, a TableView is loaded with 50 rows (as expected as there are 50 state records in my data file) and each row is numbered 0-49. I am having trouble figuring out how to access the state name in my StateList array.
Any help along the way would be much appreciated!
You would populate your tableview’s cells with data in
cellForRow... Here is a slight modification of the default implementation in new projects:EDIT The reason your app is now crashing is the object for the key
"states"is a dictionary (as it should be according to thejsonat the link you posted) and you cannot simply cast it to an array. You can, however, ask the dictionary for an array of all of the keys. Which in this case will be the state names.Change this line of code in
buildStateList: