Hi I have a master array that populates the tableview and I have a filtered array for the search results. Both of these work fine using the below method. My tableview and search display array is working well except for the below issue under this code block.
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (product *new in parserData)
{
//Description scope
if ([scope isEqualToString:@"Description"])
{
NSRange result = [new.description rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (result.location != NSNotFound)
{
[self.filteredListContent addObject:new];
}
}
//Product scope
if ([scope isEqualToString:@"Product"])
{
NSRange result = [new.item rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (result.location != NSNotFound)
{
[self.filteredListContent addObject:new];
}
}
}
}
What I would like to achieve is this. I have an Item in the master array called LMD-2451 TD Professional 3D LCD Monitor. I can search for TD Professional, TD Pro, TD and it returns the correct item mentioned above using any case. However if I search Pro TD it doesn't show any result. The problem I face is the user may not know the order the product title or description might be? So I would need to implement something that would do this logically. I am really stuck with what to do.
For reference
NSMutableArray *parserData; // Complete main full Array NSMutableArray *filteredListContent; // Array for the Search-results
Any suggestions would be greatly appreciated.
How about using componentsSeperatedByString:@" "
on your search string to split it into (in your example) an array of 2 strings, @"Pro"
and @"TD"
. Then loop through the array using the rangeOfString:
check that all the components in the array are found in new.item
.
//Product scope
if ([scope isEqualToString:@"Product"])
{
// Split into search text into separate "words"
NSArray * searchComponents = [searchText componentsSeparatedByString: @" "];
BOOL foundSearchText = YES;
// Check each word to see if it was found
for (NSString * searchComponent in searchComponents) {
NSRange result = [new.item rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
foundSearchText &= (result.location != NSNotFound);
}
// If all search words found, add to the array
if (foundSearchText)
{
[self.filteredListContent addObject: new];
}
}