KAR KAR - 2 months ago 21
iOS Question

AFNetworking tutorial of raywenderlich not displaying data on table cell

I am learning AFNetworking using,

https://www.raywenderlich.com/59255/afnetworking-2-0-tutorial

But, For json part I am not getting data displayed on cell that displyed in this tutorial.

I am getting response in json format but after that nothing is displayed in cells.

static NSString * const BaseURLString = @"http://www.raywenderlich.com/demos/weather_sample/";

@interface WTTableViewController ()
@property(strong) NSDictionary *weather;
@end

@implementation WTTableViewController

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

- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.toolbarHidden = NO;

[self.tableView setDelegate:self];
self.tableView.dataSource=self;
// 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)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"WeatherDetailSegue"]){
UITableViewCell *cell = (UITableViewCell *)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

WeatherAnimationViewController *wac = (WeatherAnimationViewController *)segue.destinationViewController;

NSDictionary *w;
switch (indexPath.section) {
case 0: {
w = self.weather.currentCondition;
break;
}
case 1: {
w = [self.weather upcomingWeather][indexPath.row];
break;
}
default: {
break;
}
}
wac.weatherDictionary = w;
}
}

#pragma mark - Actions

- (IBAction)clear:(id)sender
{
self.title = @"";
self.weather = nil;
[self.tableView reloadData];
}

- (IBAction)jsonTapped:(id)sender
{
NSString *string = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
NSURL *url = [NSURL URLWithString:string];
//NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:string parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];

}];

}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(!self.weather)
return 1;

switch (section) {
case 0: {
NSLog(@"return 1");
return 1;
}
case 1: {
NSArray *upcomingWeather = [self.weather upcomingWeather];
NSLog(@"return upcomingWeather");
return [upcomingWeather count];
}
default:
return 0;
}
}

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


NSDictionary *daysWeather = nil;

switch (indexPath.section) {
case 0: {
daysWeather = [self.weather currentCondition];
NSLog(@"currentCondition");
break;
}

case 1: {
NSArray *upcomingWeather = [self.weather upcomingWeather];
daysWeather = upcomingWeather[indexPath.row];
NSLog(@"upcomingWeather");
break;
}

default:
break;
}

cell.textLabel.text = [daysWeather weatherDescription];
NSLog(@"textLabel = %@", cell.textLabel.text);
// Configure the cell...


return cell;
}


I have this code in my table view controller.

This methods are used in delegate method in above code :

- (NSDictionary *)currentCondition
{
NSDictionary *dict = self[@"data"];
NSArray *ar = dict[@"current_condition"];
return ar[0];
}

- (NSArray *)upcomingWeather
{
NSDictionary *dict = self[@"data"];
return dict[@"weather"];
}


I am getting response in json formate but cells are empty.
Please refer tutorial if anything is missing here.

Thank you.

Answer

In success of data received you just printing data. you have to store that data in dictionary and reload tableview.

Use below code:

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string parameters:nil progress:nil 
    success:^(NSURLSessionTask *task, id responseObject) {

        self.weather = (NSDictionary *)responseObject; //add this line
        [self.tableView reloadData];                   //add this line

    } failure:^(NSURLSessionTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];

    }];