Reputation: 221
I'm having this problem. I'm trying to build an UITableView with data from a JSON Dictionary made with NSJSONSerialization. I have no idea where to start. i've been trying for 3 days but silo have achieved nothing. This is the code i've came up with:
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kJsonURL [NSURL URLWithString: @"http://rs-hosting.nl/panel/serverstatus_json.php"]
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:kJsonURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSDictionary *clients = [json objectForKey:@"announcements"];
NSArray *announcements = [clients objectForKey:@"announcement"];
NSDictionary* announcement = [announcements objectAtIndex:1];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
@end
I'm able to display content in a label on a single view in a label but i have no clue how to het all the data in the json to a table. Can someone please healp me out?
SDK: 5.1 OSX: 10.7.3 XCODE: 4.3.1
Tnx in advance
Upvotes: 0
Views: 819
Reputation: 502
after getting data in NSDictionary, save it in NSArray filled with NSString objects that u want to show and then call the function [Tableview reloadData]; in you ViewDidLoad function and ur good to go.
Upvotes: 1
Reputation: 9860
You need to store your data somewhere (keeping that NSDictionary with the whole json file is fine for a start) and then add a UITableView, set it up to point its data source to your class and then implement the UITableViewDataSource methods as outlined.
The UITableView will then request data from these methods as it fills in the table. The only two required methods are tableView:numberOfRowsInSection:
and tableView:cellForRowAtIndexPath:
so start with those two and get the data displaying on the table before doing any other customization.
Upvotes: 1