[英]conditional table view cell display
我想仅在我的AFNetworking请求将json对象返回为true时才显示tableview单元格。 在这个例子中,我需要将place =“Store”设置为true,以便显示仅显示商店的表视图。
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";
但不确定如何。
如果您有所有结果的工作代码,那么结果子集的工作代码很简单。 用[self filteredLocationResults]
替换对self.location_results
引用,实现如下:
- (NSArray *)filteredLocationResults {
return [self.location_results filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"(%K like %@)". @"place" ,@"Store"]];
}
您可以将“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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.