Cliff
Cliff

Reputation: 713

Displaying Json result in UITableView

I am trying to put my JSON results in my UITableView. I got the JSON but I cannot put them to my tableview. Any idea what I am doing wrong?

Here's some relevant code:

- (void)viewDidLoad {
    TitlosArray = [[NSMutableArray alloc] init];
    KeimenoArray = [[NSMutableArray alloc] init];
    [super viewDidLoad];
    [self fetchTweets];
}

My JSON parser is working fine:

- (void)fetchTweets {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(queue,  ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://myselection.gr/support3/frontistiria_project/android_data.php/?type=publicNews"]];

        [self performSelectorOnMainThread:@selector(fetchedForecast:)withObject:data waitUntilDone:YES];
    });
}

- (void)fetchedForecast:(NSData *)responseData {
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSLog(@"Rows %@", [json objectForKey:@"total_rows"]);
    NSArray *items = [json valueForKeyPath:@"content"];

    NSEnumerator *enumerator = [items objectEnumerator];
    titlosjson.text=[[items objectAtIndex:1] objectForKey:@"title"];
    Keimenojson.text=[[items objectAtIndex:1] objectForKey:@"descr"];

    while (item = (NSDictionary*)[enumerator nextObject]) {
        [TitlosArray addObject:[item objectForKey:@"title"]];
        [KeimenoArray addObject:[item objectForKey:@"descr"]];
        NSLog(@"Title = %@", [item objectForKey:@"title"]);
        NSLog(@"Descr = %@",[item objectForKey:@"descr"]);
    }

    NSLog(@"%@",[TitlosArray objectAtIndex: 1]);
    myArray = [NSArray arrayWithArray:TitlosArray ];
    NSLog(@"%@", [myArray objectAtIndex: 2]);
}

But I have problem with my UITableView:

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

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //cell.textLabel.text = [NSString stringWithFormat:@"by %@", text];
    return cell;
}

Upvotes: 0

Views: 1687

Answers (3)

Rok Jarc
Rok Jarc

Reputation: 18865

This works, tested.

Made from sample single-view app, tableView added in IB (and connected to IBOutlet)

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *titlosArray;
    NSMutableArray *keimenoArray;
}

@property (strong) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    titlosArray = [[NSMutableArray alloc] init];
    keimenoArray = [[NSMutableArray alloc] init];
    [super viewDidLoad];
    [self fetchTweets];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)fetchTweets
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(queue,  ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://myselection.gr/support3/frontistiria_project/android_data.php/?type=publicNews"]];

        [self performSelectorOnMainThread:@selector(fetchedForecast:)withObject:data waitUntilDone:YES];
    });
}

- (void)fetchedForecast:(NSData *)responseData
{
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSLog(@"Rows %@", [json objectForKey:@"total_rows"]);

    NSArray *items = [json valueForKeyPath:@"content"];

    //clear the arrays (if you're planning to download data more then once - refreshing...)

    [titlosArray removeAllObjects];
    [keimenoArray removeAllObjects];

    for (NSDictionary *item in items)
    {
        [titlosArray addObject:[item objectForKey:@"title"]];
        [keimenoArray addObject:[item objectForKey:@"descr"]];
    }

    //once you updated youd data-model you have to reload it
    [self.tableView reloadData];
}

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

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    cell.textLabel.text = (NSString *)[titlosArray objectAtIndex:indexPath.row];

    return cell;
}

@end

enter image description here

EDIT:

If you also want to see the details in subtitle you can change cellForRowAtIndexPath: to:

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = (NSString *)[titlosArray objectAtIndex:indexPath.row];

    cell.detailTextLabel.text=(NSString *)[keimenoArray objectAtIndex:indexPath.row];

    return cell;
}

enter image description here

Note that if you want to show the complete texts (since your query returns quite a lot of text of each record) you will have to fiddle with custom UITableViewCell implementation.

You might find this tutorial interesting: Table View Animations and Gestures

Upvotes: 4

rptwsthi
rptwsthi

Reputation: 10172

I am altering your code here, (hope you wouldn't mind) try replacing your code with this:

- (void)fetchedForecast:(NSData *)responseData {
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSLog(@"Rows %@", [json objectForKey:@"total_rows"]);
    NSArray *items = [json valueForKeyPath:@"content"];

    NSEnumerator *enumerator = [items objectEnumerator];
    titlosjson.text=[[items objectAtIndex:1] objectForKey:@"title"];
    Keimenojson.text=[[items objectAtIndex:1] objectForKey:@"descr"];
    myArray = [[NSMutableArray alloc] initWithCapacity:0];
    while (NSDictionary *item = (NSDictionary*)[enumerator nextObject]) {
        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [item objectForKey:@"title"], @"title",
                                    [item objectForKey:@"descr"], @"descr", nil];
        [myArray addObject:dictionary];
    }
}
//But I have problem with my UITableView:

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

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    NSArray *myArray;
    cell.textLabel.text = [[myArray objectAtIndex:indexPath.row] valueForKey:@"title"];
    cell.detailTextLabel.text = [[myArray objectAtIndex:indexPath.row] valueForKey:@"descr"];

    return cell;
}

Here are the points:

  • In your cellForRowAtIndexPath method you are not setting any value to titleLabel
  • I have made your cellStyle to UITableViewCellStyleSubtitlethat will be able to set detail description (you can change it if you didn't like it)
  • and finally instead of taking two different array i have used your myArray (made that mutable) and added dictionary in it and then used those valu while setting values to your cell.

Upvotes: 1

rdurand
rdurand

Reputation: 7410

You are creating empty cells, so your content will not be displayed.

Try replacing your cellForRowAtIndexPath: method with this one:

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

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

    return cell;
}

Upvotes: 6

Related Questions