搜索UITableView
我有一个UITableView填充从JSON数据饲料使用NSDictionary和NSArray,我试图使用此代码搜索UITableView数据,但它不工作一些其他相关的信息基于我如何设置它是在这个问题Data from JSON Parsing straight to UITableView搜索UITableView
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.filteredCandyArray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
NSArray *tempArray = [airportsArray filteredArrayUsingPredicate:predicate];
NSLog(@" text %@", searchText);
filteredCandyArray = [NSMutableArray arrayWithArray:tempArray];
NSLog(@"NSLog %@", scope);
} 这是数据如何解析
NSString* path = @"https://api.flightstats.com/flex/airports/rest/v1/json/active?appId=532ca6af&appKey=50d6d3351e5953152b2688f10d10d276";
NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];
[_request setHTTPMethod:@"GET"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData* _connectionData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:&error];
if(nil != error)
{
NSLog(@"Error: %@", error);
}
else
{
NSMutableDictionary* json = nil;
if(nil != _connectionData)
{
json = [NSJSONSerialization JSONObjectWithData:_connectionData options:NSJSONReadingMutableContainers error:&error];
}
if (error || !json)
{
NSLog(@"Could not parse loaded json with error:%@", error);
}
else
{
routeRes = [json objectForKey:@"airports"];
airportsArray = [json objectForKey:@"airports"];
}
_connectionData = nil;
NSLog(@"connection done");
}
NSLog(@" end array %@", candyArray);
// Initialize the filteredCandyArray with a capacity equal to the candyArray's capacity
filteredCandyArray = [NSMutableArray arrayWithCapacity:[airportsArray count]];
// Reload the table
[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[[self tableView] reloadData];
可以手动建立经由环方法构建阵列,
NSMutableArray *filteredResults = [NSMutableArray new];
[airportsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj.name rangeOfString: searchText].location != NSNotFound) {
[filteredResults addObject:obj];
}
}];
self.filteredCandyArray = [NSArray arrayWithArray:filteredResults];
您将需要用您正在使用的任何对象模型替换id obj
。例如,如果您有飞机模型,则可以使用Plane *plane
代替id obj
。
我可能会在后面添加如何通过谓词使用它,但是这应该可以满足您的要求。您可以查看NSArray文档中的enumerateObjectsUsingBlock:和arrayWithArray:方法。 rangeOfString:可以在NSString文档中查看。
我不明白这是如何符合我如何访问数据。我在我的问题中添加了一些代码,以阐明我如何访问数据, – user1828081
是要通过字典值对数据进行排序,还是要搜索数据并获取特定结果? –
了我的头顶,我可以告诉你如何不使用谓词而是一个循环的滤镜阵列,如果你不介意......但如果你是然后你需要给我6小时才能完成回家XD –
我没有设置任何方法,如果你有更好的方法我打开建议 – user1828081
我张贴了一个答案与我的想法:3告诉我如果那工作 –