Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7488921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:06:38+00:00 2026-05-29T15:06:38+00:00

I will preface the question with I am new to iOS and could use

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:06:39+00:00Added an answer on May 29, 2026 at 3:06 pm

    You would populate your tableview’s cells with data in cellForRow... Here is a slight modification of the default implementation in new projects:

    - (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.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    
        // Configure the cell.
        NSInteger rowNumber = indexPath.row;
        NSString *stateName = [StateList objectAtIndex:rowNumber];
        cell.textLabel.text = stateName;
        return cell;
    }
    

    EDIT The reason your app is now crashing is the object for the key "states" is a dictionary (as it should be according to the json at 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:

    NSArray *jsonArray = [[json objectForKey:@"states"] allKeys];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will preface this question with the fact that I am extremely new to
I'll preface this question by stating I have historically used tables for my HTML
Let me preface this question with first stating that I'm new to GUI interfaces:
I will preface this question by saying, I do not think it is solvable.
I'll preface this question with the note that I have looked at this similar
I will preface this question with the disclaimer that I know there are many
I will preface this by I am new to XNA and I realize what
Let me preface this by saying I am extremely new to git but have
I will preface the question by saying that I am somewhat new to the
I'll preface this with I'm really new to working on Android. So I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.