user1724168
user1724168

Reputation:

Data not populating on TableView

I am fetching data from the website and loading on the tableViewController. Tableviewcontroller is inside the tabbarcontroller. Whenever I clickked on tabbar, tableview data does not populated. However once I click other viewcontrollers and then click again on tableviewcontroller, then data populated.

#import "GetBookViewController.h"
#import "AFNetworking.h"

@interface GetBookViewController ()

@end

@implementation GetBookViewController
@synthesize booksArray;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

}

-(void)viewWillAppear:(BOOL)animated
{

    [self loadData];

}

-(void)viewDidAppear:(BOOL)animated
{

    [self.tableView reloadData];


}

-(void) loadData
{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:@"http://XXXXXX.com/coursera/books.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        if ([[responseObject valueForKey:@"status"] isEqualToString:@"success"]) {
            int count = [[responseObject valueForKey:@"total"] integerValue];
            NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
            for (int i = 1; i <= count; i++) {
                NSString *obj = [NSString stringWithFormat:@"%i", i];
                [array addObject:[responseObject objectForKey:obj]];
            }
            booksArray = array;
            for (id obj in booksArray) {
                NSLog(@"%@", [obj valueForKey:@"title"]);
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

}

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

#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 [booksArray count];
}

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

    UILabel* label = (UILabel*)[cell viewWithTag:100];
    NSString *title = [[booksArray objectAtIndex:indexPath.item] valueForKey:@"title"];
    label.text = title;

    return cell;
}

Upvotes: 0

Views: 53

Answers (1)

Daniel Galasko
Daniel Galasko

Reputation: 24237

You aren't doing anything once you receive a response from the network and populate your array?

What you need to do is notify the table view that it needs to query its data source again to refresh its values. Simply calling reloadData on your table view once you have your array would to the trick:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://ilyasuyanik.com/coursera/books.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
    if ([[responseObject valueForKey:@"status"] isEqualToString:@"success"]) {
        int count = [[responseObject valueForKey:@"total"] integerValue];
        NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
        for (int i = 1; i <= count; i++) {
            NSString *obj = [NSString stringWithFormat:@"%i", i];
            [array addObject:[responseObject objectForKey:obj]];
        }
        dispatch_async(dispatch_get_main_queue,^{
            booksArray = array;
            for (id obj in booksArray) {
                NSLog(@"%@", [obj valueForKey:@"title"]);
            }
           //now you can update your table view
           [self.tableView reloadData];
        });
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Upvotes: 1

Related Questions