Reputation: 5844
I want to display json data from a URL. Everything Works the data array is accessible in only did load section. When I use it to count count it gives NULL but inside did load it works. What is the issue.
.h file
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSDictionary *dictArray;
NSString *title;
}
.m file
#import "ViewController.h"
#import "AFNetworking.h"
#import "AFHTTPRequestOperation.h"
@interface ViewController ()
{
NSArray *data;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:[NSString stringWithFormat:@"sampleUrl"]
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *weather = (NSDictionary *)responseObject;
data = [weather objectForKey:@"slider"];
NSLog(@"%@",data); // Works Fine
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather" message:[NSString stringWithFormat:@"%@", error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [data count]; // doesn;t work
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
dictArray = [data objectAtIndex:indexPath.row];
cell.textLabel.text = [dictArray objectForKey:@"title"];
NSLog(@"%@",data); // Doesn;t work displays NULL
return cell;}
@end
Upvotes: 0
Views: 534
Reputation: 12344
In success block of AFNetworking manager, reload your tableView. Table is now loading before the web service gets response.
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *weather = (NSDictionary *)responseObject;
data = [weather objectForKey:@"slider"];
NSLog(@"%@",data); // Works Fine
[yourTableView reloadData];
}
Upvotes: 1