0

場所を生成する Rails API と、それらの場所のメニューがあります。私の iOS アプリでは、AFNetworking を使用して、これらの場所の json リストをテーブル ビューで取得しています。

これは、位置データを取得するために使用する AFNetworking リクエストです

LocationTableViewController.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

ユーザーが場所のセルをタップすると、次のテーブル ビューで発生する次の AFNetworking 要求に location_id を挿入するにはどうすればよいですか?

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 に答える