How to make a search bar as an iPhone contacts app or a search bar that handles more than one incomplete text
I am creating a search bar that should behave like the iPhone contacts search bar. At the same time, it is partially. here is my code
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSRange whiteSpaceRange = [result rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
if (whiteSpaceRange.location != NSNotFound)
{
NSString * stringAfterSpace = [result substringFromIndex:whiteSpaceRange.location];
NSString * stringBeforeSpace= [result substringToIndex:whiteSpaceRange.location];
NSPredicate *predicateForAfterSpace=[NSPredicate predicateWithFormat:@"catNm CONTAINS[cd] %@",stringAfterSpace];
NSPredicate *predicateForBegining = [NSPredicate predicateWithFormat:@"catNm BEGINSWITH[cd] %@", stringBeforeSpace];
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicateForBegining, predicateForAfterSpace]];
searchArray = [[dataArray filteredArrayUsingPredicate:compoundPredicate] mutableCopy];
}
else
{
NSPredicate *predicateForBegining = [NSPredicate predicateWithFormat:@"catNm BEGINSWITH[cd] %@", result];
NSString *stringAfterSpace=[NSString stringWithFormat:@" %@",result];
NSPredicate *predicateForAfterSpace=[NSPredicate predicateWithFormat:@"catNm CONTAINS[cd] %@",stringAfterSpace];
NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicateForBegining,predicateForAfterSpace]];
searchArray = [[dataArray filteredArrayUsingPredicate:compoundPredicate] mutableCopy];
}
}
The main difference is that my searchBar processes one incomplete keyword, but not more than one. Suppose array={{catNm=@"Asus Zenfone 3 Max"},{catNm=@"Asus Zenfone Go}}
if I search for asu zen
or zen ma
then it shows the result, but if search asus zen max
then it goes wrong All I want to know is how to handle multiple spaces in searchBar
source to share
Here's what you can use: Regular Expression (Regex).
NSString *keyToFilter = @"catNm";
NSArray *dataArray = @[@{keyToFilter: @"Asus Zenfone 3 Max"},
@{keyToFilter: @"Asus Zenfone Go"}];
NSArray *wordsSearched = [searchString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableString *pattern = [[NSMutableString alloc] initWithString:@".*\\b"];
[pattern appendString:[wordsSearched componentsJoinedByString:@".*\\b"]];
[pattern appendString:@".*"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.%K MATCHES[C] %@", keyToFilter, pattern];
NSArray *filtered = [dataArray filteredArrayUsingPredicate:predicate];
NSLog(@"SearchString: %@\nFiltered: %@", searchString, ([filtered count]?[filtered valueForKey:keyToFilter]:@"NO RESULT"));
I'm not very sure about regex, but this should do it. You can use online RegexTools if you want to add more complex stuff.
Test results / example:
$>SearchString: "asus zen max"
Filtered: (
"Asus Zenfone 3 Max"
)
$>SearchString: "zen ma"
Filtered: (
"Asus Zenfone 3 Max"
)
$>SearchString: "asu zen"
Filtered: (
"Asus Zenfone 3 Max",
"Asus Zenfone Go"
)
$>SearchString: "zen"
Filtered: (
"Asus Zenfone 3 Max",
"Asus Zenfone Go"
)
$>SearchString: "M"
Filtered: (
"Asus Zenfone 3 Max"
)
$>SearchString: "sams"
Filtered: NO RESULT
$>SearchString: "zen asu"
Filtered: NO RESULT
$>SearchString: "zen g"
Filtered: (
"Asus Zenfone Go"
)
$>SearchString: "a g"
Filtered: (
"Asus Zenfone Go"
)
source to share