0

I want to display a tableview cell only if my AFNetworking request returns a json object as true. In this example I need place = "Store" to be true in order to display a table view which displays only stores.

the place = "Store" json is returned as part of my location_results array in the following request and I store it with self.place = [dictionary objectForKey:@"place"];

- (void)viewDidLoad
{
    [super viewDidLoad];


    // LoadLocations

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

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

                                    }];

I know I need to start by changing

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.location_results.count;
}

but not sure how.

Then I should be able to add a conditional statement to

- (LocationCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"LocationCell";

but not sure how.

4

2 に答える 2

1

すべての結果の作業コードがある場合、結果のサブセットの作業コードは単純です。self.location_resultsへの参照を、[self filteredLocationResults]次のように実装された に置き換えます。

- (NSArray *)filteredLocationResults {

    return [self.location_results filteredArrayUsingPredicate:
        [NSPredicate predicateWithFormat:@"(%K like %@)". @"place" ,@"Store"]];
}
于 2013-03-29T22:39:57.007 に答える
1

「Store」アイテムをテーブルのデータ ソースに入れるだけです。

for (id locationDictionary in response) {
    Location *location = [[Location alloc] initWithDictionary:locationDictionary];
    if([[location objectForKey:@"place"] isEqualToString:@"Store"]) // Add this line
        [location_results addObject:location];

}
于 2013-03-29T23:22:04.020 に答える