iOS的新手。我正在使用AFNetworking下载JSON数据并将数据放入Dictionary对象中。这在下面的代码完成:如何使用AFNetworking使用填充了JSON数据的NSDictionary对象填充原型单元格文本标签
-(void)locationRequest
{
NSURL *url = [NSURL URLWithString:@"http://sitespec/php/getlocations.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//AFNetworking asynchronous url request
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject)
{
self.locationsFromJSON = (NSDictionary *)responseObject;
NSLog(@"JSON RESULT %@", responseObject);
NSLog(@"JSON DICTIONARY %@", _locationsFromJSON);
[self.tableView reloadData];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseObject)
{
NSLog(@"Request Failed: %@, %@", error, error.userInfo);
}];
[operation start];
}
我得到的数据和填充字典阵列作为的NSLog的outpur看到以下称:
2013-05-12 12:44:08.851 sitespec[2024:c07] JSON RESULT (
{
city = "Rocky Mount";
id = 1;
name = "Rocky Mount";
phone = "(252) 451-1811";
state = NC;
"street_address" = "620 Health Drive";
zip = 27804;
},
{
city = "Elizabeth City";
id = 2;
name = "Elizabeth City";
phone = "(252) 335-4355";
state = NC;
"street_address" = "109 Corporate Drive";
zip = 27906;
}
)
2013-05-12 12:44:08.852 sitespec[2024:c07] JSON DICTIONARY (
{
city = "Rocky Mount";
id = 1;
name = "Rocky Mount";
phone = "(252) 451-1811";
state = NC;
"street_address" = "620 Health Drive";
zip = 27804;
},
{
city = "Elizabeth City";
id = 2;
name = "Elizabeth City";
phone = "(252) 335-4355";
state = NC;
"street_address" = "109 Corporate Drive";
zip = 27906;
}
)
,但这里是我失去了.....
如何我在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
满足访问Dictionary对象这个数据HOD?
类似:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.locationsFromJSON objectForKey:@"name"];
???
这不起作用,我得到一个SIGBART错误。我只是不知道如何读取数据并填充表视图?
我该怎么做?任何帮助非常感谢.....
我创建了一个地址模型,并添加了我的JSON数据的所有属性。我已经将Address.h文件导入到我的tableView.m文件中,我在那里下载数据并希望显示tableView。我有一个名为locationsFromJSON的数组,定义如上所述。尽管如此,你说“当你解析你的JSON时,你创建了Address类实例,你将添加到数组中”我在返回的值中有JSON响应:responseObject。我如何创建Address类实例并将它们添加到数组中? – 2013-05-12 18:15:25
请检查我编辑的答案 – danypata 2013-05-12 18:31:44