I am doing a search screen (to search list of names) on my iPhone app, where I need to search by both first name and last name.
Say my search tableview contains names like follows
Ravi Kiran
sujay huilgol
harry potter
My search is working for only first name i.e if I search for Ra it will show Ravi Kiran but when I search for Kiran it's not displaying Ravi Kiran.
My filterContentForSearchText
is like this:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.arrSearhResults removeAllObjects]; // First clear the filtered array.
// Search the main list for Usernames whose type matches searchText; add items that match to the filtered array.
for(NSString * strUserName in arrUserInfo )
{
NSComparisonResult result = [strUserName compare:searchText options:(NSLiteralSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
// Update the filtered array based on the search text and scope.
[self.arrSearhResults addObject:strUserName];
//NSLog(@"arrSearhResults %@",arrSearhResults);
}
}
}
What can I try to resolve this?
use rangeOfString to search whole string
for(NSString * strUserName in arrUserInfo )
{
NSRange range = [strUserName rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (range.length > 0)
{
[self.arrSearhResults addObject:strUserName];
}
}