Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

JSON problem

Hey i got a problem in my code ... there are no errors but DATA doesn't show !

#import "jobListViewController.h"


@interface jobListViewController ()

@end

#define getDataURL @"http://localhost/Rany/json.php"

@implementation jobListViewController
@synthesize json, jobsArray, myTableView;


- (void)viewDidLoad
{
    self.title = @"Rany";
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [super viewDidLoad];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return jobsArray.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:@"cell %d", indexPath.row];

    Jobs * currectJob = [jobsArray objectAtIndex:indexPath.row];

    cell.textLabel.text = currectJob.jobName;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


}

- (void) retrieveData
{
    NSURL * url = [NSURL URLWithString:getDataURL];
    NSData * data = [NSData dataWithContentsOfURL:url];

    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    jobsArray = [[NSMutableArray alloc]init];

        for (int i = 0; i < json.count; i++)
        {
            NSString * jID = [[json objectAtIndex:i] objectForKey:@"id"];
            NSString * jName = [[json objectAtIndex:i] objectForKey:@"jobName"];
            NSString * jTime = [[json objectAtIndex:i] objectForKey:@"jobTime"];
            NSString * jDesc = [[json objectAtIndex:i] objectForKey:@"jobDesc"];
            NSString * jNeed = [[json objectAtIndex:i] objectForKey:@"jobNeed"];

            Jobs * myJob = [[Jobs alloc] initWithjobID: (NSString *) jID andjobName: (NSString *) jName andjobTime: (NSString *) jTime andjobDesc: (NSString *) jDesc andjobNeed: (NSString *) jNeed];

            [jobsArray addObject:myJob];


        }


}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}

Please help me !!

1 Answer

Alessandro Covre
Alessandro Covre
7,057 Points

You have implemented "retrieveData" method, but I can't see the call of that method :) Is it possible that you didn't call the method and so the json content is not retrieved?