I have a tableview that filters based on a search bar. This works well as long as the search begins with the same characters as the cell text.
I would like to be able to search any part of the cell text, even in the middle. For example. If the cell text is “Jon Bon Jovi” I would like it to still come up if the user types “Bon Jovi” or even just “Jovi” into the search bar.
My current code is below:
for (NSDictionary *item in listItems)
{
if ([scope isEqualToString:@"All"] || [[item objectForKey:@"type"]
isEqualToString:scope] || scope == nil)
{
NSComparisonResult result = [[item objectForKey:@"name"]
compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[filteredListItems addObject:item];
}
}
}
Any help would be awesome. Thank you all!
Instead of using
-compare:options:range:(which just sorts your search string relative to your item’s name), consider looking at-rangeOfString:options:– that will actually search the entire item name for your search string. You would replace the inner body of yourifstatement with something like: