Jimmie Magnusson Jimmie Magnusson - 1 month ago 14
iOS Question

JSON to Tableview

I've been trying to append JSON data from a webserver into a tableviewcontroller. This is my current code.

The NSlog of _datalist prints the JSON, but the other methods for actually putting the data in cells, does not.. The code looks correct, and since the _datalist NSarray actually gets the data, I'm not sure how to handle this.. Which is why I need your help and advice.

As for what I've figured out, the method where the cells are initialized with the JSON data, can't either fetch it, or use it. My clue was that I need to encode the incoming data?

#import "TopUsersViewController.h"

@interface TopUsersViewController ()
@property NSArray* dataList;
@end

@implementation TopUsersViewController




- (void)viewDidLoad
{
[super viewDidLoad];

[self readDataFromFile];

[self.tableView reloadData];
}


-(void)readDataFromFile
{


NSString *urlAsString = @"http://myURL.com/json.json";

NSURL *url = [[NSURL alloc] initWithString:urlAsString];
NSLog(@"%@", urlAsString);

[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

if (error)
{
// no internet / can't fetch!
NSLog(@"Failure %@",error.localizedDescription);


}


NSLog(@"Success!");


dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"works.");

self.dataList = (NSArray *)[NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];

// this NSlog prints the JSON data successfully.
NSLog(@"oo%@ and array %@",_dataList,_array);




});
}];







}


// none of these methods are running..

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// count the initialized array from viewDidLoad
return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"return the data");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

id keyValuePair = self.dataList[indexPath.row];


cell.textLabel.text = keyValuePair[@"identity"];

cell.detailTextLabel.text=[NSString stringWithFormat:@"Points: %@", keyValuePair[@"points"]];
return cell;
}


@end

Answer

You need to tell your tableView by a delegate method that you want to reload your tableView when you have data in your array...

dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"works.");

            self.dataList = (NSArray *)[NSJSONSerialization JSONObjectWithData:data
                                                         options:0
                                                           error:nil];

            [self.tableView reloadData];
            // this NSlog prints the JSON data successfully.    
            NSLog(@"oo%@ and array %@",_dataList,_array);




            });

You are reloading your tableView in viewDidLoad Method while at that point your array has no element and when the success block of your api call is executed then you need to call reloadData method.