我在
IOS中很新,我正在使用UISearchDisplayController进行搜索.
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF == %@", searchText];
NSArray *filtered = [self.arrProductList filteredArrayUsingPredicate:predicate];
NSLog(@"%@", filtered);
}
这是我的self.arrProductList是一个数组数组,即
({ ID = 1; description = "Coalesce Functioning on impatience T-Shirt"; pname = "Coalesce Functioning T-Shirt"; price = "299.00"; qty = 99; }, { ID = 2; description = "Eater Krylon Bombear Destroyed T-Shirt"; pname = "Girl's T-Shirt"; price = "499.00"; qty = 99; }, { ID = 3; description = "The Get-up Kids Band Camp Pullover Hoodie"; pname = "Band Camp T-Shirt"; price = "399.00"; qty = 99; })
我的问题是如何使用键“pname”进行搜索?我的应用程序崩溃了
filteredArrayUsingPredicate:
最佳答案 您需要修改谓词以添加要查找的键:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pname == %@", searchText];
另外==将实际寻找完全匹配,即如果您将进入Band Camp T-Shirt作为搜索,那么您将得到结果,如果您只是进入乐队或营地或衬衫,您将无法获得任何结果.因此,为了实现基于字符的搜索,您需要修改谓词以包含contains关键字.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pname contains[cd] %@", searchText];
[cd]将匹配不区分大小写.