0

I am new to IOS i need to display json fetched data into uitableview my json pattern look like this.

{"output":[{"id":{"php":"1"},"name":{"php":"ramkumar"},"image":{"1":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}},{"id":{"php":"2"},"name":{"php":"rakshi"},"image":{"2":"http:\/\/www.easyteach.gr\/users\/tutor\/1440371842.jpg"}},{"id":{"android":"1"},"name":{"android":"Vijayan"},"image":{"3":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}},{"id":{"android":"2"},"name":{"android":"vishali"},"image":{"4":"http:\/\/www.easyteach.gr\/users\/tutor\/1440371842.jpg"}},{"id":{"android":"3"},"name":{"android":"Niranjan"},"image":{"5":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}}]}

viewdidload coding:

i created json url into array format in viewdidload itself and displayed successfully in nslog but in tableview it can failed.

if(data){
        NSError *error;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];


        NSLog(@"%@",json);
        nameArray = [json valueForKeyPath:@"output.name"];
        php = [nameArray valueForKey:@"php"];


    }
    [_tabledata reloadData];

Tableview delegates:

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

    return 1;
}

-(NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section{

    return [php count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];

    cell.iteam.text=[php objectAtIndex:indexPath.row];

    return cell;
}

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

}
A.sonu
  • 159
  • 10

2 Answers2

0

This is a bit of an odd question, but here's some pointers.

First, you don't mention what that php variable is, but I assume it's an NSArray. Before sending it to the UITableView, it might be worth checking how many elements you're expecting to show:

php = [nameArray valueForKey:@"php"];
NSLog(@"There are %d items to be displayed", (int)php.count);

Next, I had an issue a few years ago where my UITableView wasn't triggering my various UITableView delegate functions.

Have a read of this article to see what I needed to do, and try adding some breakpoints on your delegate functions to see if they are being called.

And finally, your cellForRowAtIndexPath function seems to create a new TableViewCell item every time:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];

    cell.iteam.text=[php objectAtIndex:indexPath.row];

    return cell;
}

There should be some code in there to only create a new cell if the OS can't reuse an existing object:

TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];
if (cell == nil) {
    cell = TableViewCell *cell=[[CompanyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"table"];
}

I would also be tempted to slip in a line OF code, to set the default textLabel, and see if anything appears:

cell.textLabel.font = [php objectAtIndex:indexPath.row];

Hope some of this helps...

Update

Actually, I reckon you're misinterpreting your JSON string.

Your JSON contains one element, output, which is an array of records.

So (I haven't tried this) I reckon you should be defining php like this:

    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];
    php = [json valueForKey:@"output"];

That should give you the array for your UITableView to work with. But from there, you'll need to access the relevant elements within each record.

{
    "id": {
            "php": "1"
    },
    "name": {
        "php": "ramkumar"
    },
    "image": {
        "1": "http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"
    }
}

At the moment, your cellForRowAtIndexPath function would set the text to the entire record:

cell.iteam.text=[php objectAtIndex:indexPath.row];
Community
  • 1
  • 1
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
-1

Use below line of code. Hope it will help you:

if(data){
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];


    NSLog(@"%@",json);
    nameArray = [json valueForKeyPath:@"output.name"];
    php = [[nameArray valueForKey:@"php"] mutableCopy];
 [php removeObjectIdenticalTo:[NSNull null]];


}
[_tabledata reloadData];
Payal Maniyar
  • 4,293
  • 3
  • 25
  • 51