2

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 :)

12
  • 1
    At the end of your parsing, try a reloadData on your TableView Commented Apr 26, 2014 at 8:41
  • I added [tableView reloadData]; in the end of the UITableViewCell Method, nothing happens.. Commented Apr 26, 2014 at 9:01
  • why don't you use jsonObjects directly and forgo myObject since myObject is, technically, no different than jsonObjects. plus... why do you use objectForKeyedSubscript, i'd suggest using objectForKey instead? (not sure but these suggestions might not really help you with the core issue but would be an improvement atleast) Commented Apr 26, 2014 at 9:07
  • also... does jsonSource have valid contents? set a breakpoint at the end of -viewDidLoad and check if jsonSource and myObject look like how you'd expect them to look like Commented Apr 26, 2014 at 9:14
  • At the end of the ViewDidLoad myObject has 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.. Commented Apr 26, 2014 at 9:29

2 Answers 2

1

viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURL *url = [NSURL URLWithString:@"http://example.com/JSON/index.php"];
    NSData *jsonSource = [NSData dataWithContentsOfURL:url];

    //store the JSON into an array
    myObject = [[NSMutableArray alloc] init];
    myObject = [NSJSONSerialization JSONObjectWithData:jsonSource 
                                options:NSJSONReadingMutableContainers error:nil];
    [self.tableView reloadData];
}

numberOfSectionsInTableView

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

numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return myObject.count;
}

cellForRowAtIndexPath

- (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 * obj = myObject[indexPath.row];
    cell.textLabel.text = obj[@"name"] ;
    cell.detailTextLabel.text= obj[@"details"] ;

    return cell;
}
Sign up to request clarification or add additional context in comments.

4 Comments

i tested your solution, the table is still empty, but thanks for your idea
ok just come back to work, i'm now using a different json php script, and its working! I haven't tested it yet if its working directly with my code but your code is working. So I have to spell a very great THANKS :) you did a great Job.
@Criska nice , Good luck with your app ;-)
@meda : Criska's issue was an invalid json which had been pointed out in the question comments. What you suggested was just a more efficient way of doing things but would not have solved his problem because myObject would have remained nil until the json was fixed (...all of which was pointed out in the comments) I was just waiting to be sure about the OP's core issue before posting an answer.
0

You're missing quotes for the key name in your json response.

[
  {
    "name": "XYZ",
    "details": "XYZ"
  },
  {
    "name": "XYZ",
    "details": "XYZ"
  }
]

Also, for improvement sake, use jsonObjects directly (currently you are reading jsonObjects and making myObject which is no different than jsonObjects itself)

And... since you're using a prototype cell, your current way of doing things in -cellForRowAtIndexPath: is wrong.
i.e: Currently, you have an if (cell == nil) block which... is useless (with prototype cells) and you will end up seeing only cell.textLabel.text.

Proper way:

  1. Go to storyboard
  2. Select the protoype cell
    • (via Interface Builder on the right) Change it's style from custom to subtitle

Finally...

Example:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSURL *url = [NSURL URLWithString:@"http://example.com/JSON/index.php"];
    NSData *jsonSource = [NSData dataWithContentsOfURL:url];

    //declare NSArray *jsonObjects; in the .h
    jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource 
                                                  options:NSJSONReadingMutableContainers 
                                                    error:nil];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Item";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //useless when using a prototype cell
    //if (cell == nil) {
    //    cell=[[UITableViewCell alloc]initWithStyle: UITableViewCellStyleSubtitle 
    //                               reuseIdentifier:CellIdentifier];
    //}

    //fyi: old style
    //NSString *strName = [[jsonObjects objectAtIndex:indexPath.row] objectForKey:@"name"];
    //NSString *strDetails = [[jsonObjects objectAtIndex:indexPath.row] objectForKey:@"details"];

    //new style (but doesn't matter which one you adopt)
    NSString *strName = jsonObjects[indexPath.row][@"name"];
    NSString *strDetails = jsonObjects[indexPath.row][@"details"];

    [cell.textLabel setText:strName];
    [cell.detailTextLabel setText:strDetails];

    return cell;
}

FYI: It was mainly the json that was invalid.
Your original code would have worked had your json been in the right format but surely the prototype cell would have needed to be fixed up properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.