I am pretty new in IOS and I am using UISearchDisplayController
for searching.
-(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);
}
And Here is my self.arrProductList is an array of array i.e
({ 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; })
My question is how to search using key "pname"? My app is Crashed in
filteredArrayUsingPredicate:
You need to modify your predicate to add the key to look for:
Additionally
==
will actually look for exact matches, ie if you'll enterBand Camp T-Shirt
as search then you'll get the result, you won't be able to get any result if you simply enterBand
orCamp
orShirt
. So in order to achieve the character based search, you need to modify predicate to includecontain
keyword.[cd]
will match with case insensitive.My friend solve this issue. Here, is the code: