iOS 新手。我正在使用 AFNetworking 下载 JSON 数据并将数据放入 Dictionary 对象中。这是在以下代码中完成的:
-(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];
}
我正在获取数据并填充 Dictionary 数组,如下面的 NSLOG 调用的结果所示:
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;
}
)
但是,这就是我迷路的地方......
如何在我的 Dictionary 对象中访问这些数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法?
就像是:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.locationsFromJSON objectForKey:@"name"];
???
这不起作用,我收到 SIGBART 错误。我只是不知道如何读取数据并填充表格视图?
我该怎么做呢?任何帮助是极大的赞赏.....