gpt4 book ai didi

ios - 使用 AFNetworking 从 URL 显示 JSON 数据

转载 作者:行者123 更新时间:2023-11-29 02:20:39 26 4
gpt4 key购买 nike

我想显示来自 URL 的 json 数据。一切正常,数据数组只能在加载部分访问。当我用它来计数时,它给出 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

最佳答案

在 AFNetworking 管理器的成功 block 中,重新加载您的 tableView。表现在在 Web 服务获得响应之前加载。

 success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *weather = (NSDictionary *)responseObject;

data = [weather objectForKey:@"slider"];
NSLog(@"%@",data); // Works Fine
[yourTableView reloadData];

}

关于ios - 使用 AFNetworking 从 URL 显示 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28187404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com