SaSquadge
SaSquadge

Reputation: 219

Data Not Displaying on TableViewController

I'm having an issue and not sure how to go about it, I'm getting data from my site from JSON data and I'm able to get the data and see iT when I do NSLog.The data shows up a little different then what I've done before. This is what it looks like

{
  COLUMNS =     (
    OCCUPIED,
    STATIONS,
    SITEID,
    NAME,
    LOCATION,
    ABBREVNAME,
    LATITUDE,
    LONGITUDE
    );

   DATA =     {
    LOCATION =         (
        "MH 111",
        Roy,
        Morgan,
        "SU 323",
        "SC 262",
        "D3 230",
        "SS 38 Basement",
        "SL 228"
    );
}

How do I go about displaying the MH 111 on the TableViewController. The way I've been doing it doesn't seem to work and ends up throwing a Thread 1: signal SIGBRT error every time and I can figure out why. Here is the code I have below some stuff changed just because its posted here.

Have a class that does the url and gets the JSON data

- (void)viewDidLoad {
    [super viewDidLoad];
    [self fetchSites];
}

- (void)fetchSites {
    NSURL *url = [NSURL URLWithString:@"myurlhere"];
    NSData *jsonData = [NSData dataWithContentsOfURL:url];
    NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
    NSArray *sites = [results objectForKey:@"DATA"];
    self.sites = sites;
}

Also for the tableviewcontroller class I have this

- (void)setSites:(NSArray *)sites {
    _sites = sites;
    [self.tableView reloadData];
}

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Site Cell" forIndexPath:indexPath];

NSDictionary *site = self.sites[indexPath.section];
cell.textLabel.text =[site objectForKey:@"LOCATION"];
return cell;
}

Also I have a NSArray property in the .h of the table view controller called sites, this tends to give me the thread 1: signal error and I can't seem to figure it out, could anyone help? I've also made sure the identifier is correct and also that the tableViewController class is correct.

Upvotes: 0

Views: 38

Answers (1)

rmaddy
rmaddy

Reputation: 318784

The call to [results objectForKey:@"DATA"] does not give you an array. It gives you another dictionary which has one key ("LOCATION") to the array you want.

So in fetchSites do:

NSArray *sites = results[@"DATA"][@"LOCATION"];

Then in cellForRowAtIndexPath do:

cell.textLabel.text = self.sites[indexPath.row];

Upvotes: 1

Related Questions