I have loaded JSON data from my website and added it to a UITableView but the Table is still empty.. The app doesn't crash, so I have really no idea whats wrong maybe someone of you could help me. I will give you the code below and also the connection of the FilesOwner.
Thanks in advance
My JSON Data file:
[
{ name: "XYZ", details: "XYZ"},
{ name: "XYZ", details: "XYZ"}
]
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
IBOutlet UITableView *tableData;
}
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController (){
NSMutableArray *myObject;
NSDictionary *data;
NSString *name;
NSString *details;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
name = @"name";
details = @"details";
myObject = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"http://example.com/JSON/index.php"];
NSData *jsonSource = [NSData dataWithContentsOfURL:url];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects) {
NSString *name_data = [dataDict objectForKey:@"name"];
NSString *details_data = [dataDict objectForKey:@"details"];
data = [NSDictionary dictionaryWithObjectsAndKeys:
name_data,name,
details_data,details,
nil];
[myObject addObject:data];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return myObject.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Item";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell=[[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
NSMutableString *title;
//text = [NSString stringWithFormat:@"%@",[tmpDict objectForKey:title]];
title = [NSMutableString stringWithFormat:@"%@",
[tmpDict objectForKeyedSubscript:name]];
NSMutableString *second;
second = [NSMutableString stringWithFormat:@"%@",
[tmpDict objectForKey:details]];
cell.textLabel.text = title;
cell.detailTextLabel.text= second;
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Main.storyboard:
Reuse Identifier of the Prototypecell is "Item".
dataSource is connected to ViewController
delegate is connected to ViewController
tableData is connected to ViewController
This is all I have done so... Much thanks :)
reloadDataon your TableView[tableView reloadData];in the end of theUITableViewCellMethod, nothing happens..jsonObjectsdirectly and forgomyObjectsincemyObjectis, technically, no different thanjsonObjects. plus... why do you useobjectForKeyedSubscript, i'd suggest usingobjectForKeyinstead? (not sure but these suggestions might not really help you with the core issue but would be an improvement atleast)jsonSourcehave valid contents? set a breakpoint at the end of-viewDidLoadand check ifjsonSourceandmyObjectlook like how you'd expect them to look likeViewDidLoadmyObjecthas 0 Objects in it... so there must be a parse failure, if i'm right? I also thought before that objective-c maybe couldn't read a php file correctly, but I didn't found a solution to name it like an commercial API..