仅当我的AFNetworking请求将json对象返回true时,我才想显示tableview单元格。在这个例子中,我需要place =“Store”为真,以便显示一个只显示商店的表视图。
place =“Store”json在以下请求中作为location_results数组的一部分返回,并将其与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);
}];
我知道我需要从改变
开始- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.location_results.count;
}
但不确定如何。
然后我应该能够将条件语句添加到
- (LocationCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"LocationCell";
但不确定如何。
答案 0 :(得分:1)
如果您有所有结果的工作代码,那么结果子集的工作代码很简单。将对self.location_results
的引用替换为[self filteredLocationResults]
,实现如下:
- (NSArray *)filteredLocationResults {
return [self.location_results filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"(%K like %@)". @"place" ,@"Store"]];
}
答案 1 :(得分: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];
}