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