我想显示来自网址的json数据。 Everything Works数据数组只能在load部分中访问。当我使用它来计算计数时,它会给出NULL,但内部确实加载它有效。问题是什么。
.h文件
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSDictionary *dictArray;
NSString *title;
}
.m文件
#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
答案 0 :(得分:1)
在AFNetworking管理器的成功块中,重新加载tableView。表现在在Web服务获得响应之前加载。
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *weather = (NSDictionary *)responseObject;
data = [weather objectForKey:@"slider"];
NSLog(@"%@",data); // Works Fine
[yourTableView reloadData];
}