0

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..

Vishnu
  • 2,243
  • 2
  • 21
  • 44
  • Did you log "[jsonarray objectAtIndex:indexPath.row]" ? – lu yuan Jul 22 '12 at 07:30
  • ya.I can get my json content in that [jsonarray objectAtIndex:indexPath.row].. – Vishnu Jul 22 '12 at 07:34
  • Can you set text by " cell.textLabel.text= @"test"; "? – lu yuan Jul 22 '12 at 07:36
  • How does your JSON look like? – Sven Jul 22 '12 at 07:37
  • @Sven:please follow the link in my code... – Vishnu Jul 22 '12 at 07:38
  • [{"category":" DESIGNER SAREES"},{"category":"CASUAL SAREES"},{"category":"CELEBRITY SAREES"},{"category":"DESIGNER SAREES"},{"category":"DINNER SAREES"},{"category":"EXCLUSIVE TUNICS"},{"category":"KIDS WEAR"},{"category":"MENS"},{"category":"New Category"},{"category":"SALWAR KAMEEZ"},{"category":"SAREES"}] – Vishnu Jul 22 '12 at 07:39
  • @Sven:[{"category":" DESIGNER SAREES"},{"category":"CASUAL SAREES"},{"category":"CELEBRITY SAREES"},{"category":"DESIGNER SAREES"},{"category":"DINNER SAREES"},{"category":"EXCLUSIVE TUNICS"},{"category":"KIDS WEAR"},{"category":"MENS"},{"category":"New Category"},{"category":"SALWAR KAMEEZ"},{"category":"SAREES"}] – Vishnu Jul 22 '12 at 07:39

1 Answers1

2

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.

Sven
  • 22,475
  • 4
  • 52
  • 71
  • if in case i'm having more than dictionary value like category,title,id...I need to display it all in a single cell...how can i get it... – Vishnu Jul 22 '12 at 08:04
  • Build a single string that contains them all (using `[NSString stringWithFormat:]`) or create a custom table view cell which contains multiple labels. – Sven Jul 22 '12 at 08:07
  • can u follow this question..one of my collegue asked in SO..http://stackoverflow.com/questions/11580367/uitableviewcontroller-cell-text-label-content-not-displayed – Vishnu Jul 22 '12 at 08:09