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:
My friend solve this issue. Here, is the code:
- (void) searchBarDidBeginEditing:(UISearchBar*) lclSearchBar
{
self.searchBar.showsCancelButton = YES;
}
#pragma mark - UISearchDisplayController delegate methods
-(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
{
if (searchResults.count != 0) {
[searchResults removeAllObjects];
}
for (int i=0; i< [self.arrProductList count]; i++)
{
NSString *string = [[self.arrProductList objectAtIndex:i] valueForKey:@"pname"];
NSRange rangeValue = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (rangeValue.length > 0)
{
NSLog(@"string contains bla!");
[searchResults addObject:[self.arrProductList objectAtIndex:i]];
}
else
{
NSLog(@"string does not contain bla");
}
}
NSLog(@"fiilterArray : %@",searchResults);
}