Search code examples
iphoneiosxcodeuitableviewsbjson

UItbaleview Cell json contents not displayed in iOS


I created an app which will fetch info from web service.So far i got it by displaying the contents using NSLog but when i tried to load it in UITableViewCell its not displayed.Here is my code for that

#import "TableViewController.h"
#import "JSON.h"
#import "SBJsonParser.h"

@interface TableViewController ()

@end

@implementation TableViewController

@synthesize jsonurl,jsondata,jsonarray;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    }

jsonurl=[NSURL URLWithString:@"http://minorasarees.com/category.php"];

jsondata=[[NSString alloc]initWithContentsOfURL:jsonurl];

self.jsonarray=[jsondata JSONValue];




return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [jsonarray count];
}

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

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

NSLog(@"1");
NSLog(@"%@",jsonarray);

cell.textLabel.text=[jsonarray objectAtIndex:indexPath.row];

NSLog(@"2");

return cell;
}



-(void)dealloc
{ 
[super dealloc];
[jsonarray release];
[jsondata release];
[jsonurl release];
}
@end

i've inserted tableviewcontroller by adding file with UITableViewController..will that be a problem..Help please..


Solution

  • Your JSON contains an array of dictionaries, so you're setting the text in your table view cell to an dictionary which cannot work since a string is expected. This actually should crash.

    To solve that set your text to the category property of that dictionary:

    cell.textLabel.text=[[jsonarray objectAtIndex:indexPath.row] valueForKey: @"category"];
    

    besides this there are other things wrong with your code: [super dealloc] needs to be the last thing you call in your dealloc method. And you really should be using asynchronous networking code, blocking the main thread with networking is not acceptable.