0

我有一个生成位置的 rails api,以及这些位置的菜单。在我的 iOS 应用程序中,我使用 AFNetworking 在表格视图中检索这些位置的 json 列表。

这是我用来获取位置数据的 AFNetworking 请求

位置表视图控制器.m

[[LocationApiClient sharedInstance] getPath:@"locations.json" parameters:nil
                                        success:^(AFHTTPRequestOperation *operation, id response) {
                                            NSLog(@"Response: %@", response);
                                            NSMutableArray *results = [NSMutableArray array];
                                            for (id locationDictionary in response) {
                                                Location *location = [[Location alloc] initWithDictionary:locationDictionary];
                                                [results addObject:location];

                                            }
                                            self.results = results;
                                            [self.tableView reloadData];
                                        }
                                        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                            NSLog(@"Error fetching locations!");
                                            NSLog(@"%@", error);

                                        }];

为位置请求返回的 json 示例

 {
    "created_at" = "2013-02-23T19:25:26Z";
    id = 2;
    lat = "48.870175";
    long = "2.779899999999998";
    name = "Disneyland Hotel";
    "places_id" = fd90769e2cf40d6cb6cb1a3d0bfd0ce5d37ed331;
    "street_address" = "Rue de la Marni\U00e8re, Chessy, France";
    "updated_at" = "2013-02-23T19:25:26Z";
}, 

显示姓名和地址

在此处输入图像描述

当用户点击位置单元格时,我想显示该位置啤酒列表的表格视图。rails api 路由(对于 location_id => 1)是http://localhost:3000/locations/1/beers.json并为属于 location_id:1 的啤酒输出以下 json

    {
"created_at":"2013-02-23T19:25:53Z",
"description":"fkjgvjkg",
"id":1,"location_id":1,
"name":"nhoihioh",
"price":"2.5","style":"ipa",
"updated_at":"2013-02-23T19:25:53Z"
    }

当用户点击位置单元格时,我想显示该位置的啤酒列表,通过http://localhost:3000/locations/(whatever location_id is associated with the table cell that was tapped)/beers.json

当用户点击位置单元格时,如何将 location_id 插入到下一个表视图中发生的以下 AFNetworking 请求中?

BeerTableViewController.m

[[LocationApiClient sharedInstance] getPath:@"locations/**(location_id from location table cell inserted here)**/beers.json" parameters:nil
                                        success:^(AFHTTPRequestOperation *operation, id response) {
                                            NSLog(@"Response: %@", response);
                                            NSMutableArray *results = [NSMutableArray array];
                                            for (id beerDictionary in response) {
                                                Beer *beer = [[Beer alloc] initWithDictionary:beerDictionary];
                                                [results addObject:beer];

                                            }
                                            self.results = results;
                                            [self.tableView reloadData];
                                        }
                                        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                            NSLog(@"Error fetching beers!");
                                            NSLog(@"%@", error);

                                        }];

我假设我必须记录 location_id 并在点击表格单元格时将该值作为 url 的参数传递,但我不知道该怎么做。

4

1 回答 1

1

当您使用 JSON 时,您可能应该使用 AFJSONRequestOperation,因为它会将响应数据解析为字典。

AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                           success:^(NSURLRequest *request, NSHTTPURLResponse *response, id locationJSON) {

....

};

然后,您可以从字典中获取 places_id 并再次调用以获取啤酒信息。

于 2013-02-28T16:40:41.313 回答