0

I am searching my products like the below. It works fine however when searching between 2 words the results disappear then reappear once the user has entered the first character of the next word. i.e if I search "td pro" or "pro td" the results are there. If I search like this td(i have a result) td(space) I have NO result td p(I have a result) just to add I need to separate the words by space in componentsSeperatedByString because the user may not search for a product in any particular order. Unless there is a better method?

Here is my code:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope

{

[self.filteredListContent removeAllObjects]; // First clear the filtered array.



for (XMLStringFile *new in rssOutputData_MutableArray)
{

    //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.xmlItem rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
            NSRange descriptionRange = [new.xmlDescription rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];

            foundSearchText &= (result.location != NSNotFound|| descriptionRange.location != NSNotFound);
        }

        // If all search words found, add to the array
        if (foundSearchText)
        {
            [self.filteredListContent addObject: new];
        }
    }

}

}

Many thanks

Alex McPherson
  • 3,185
  • 3
  • 30
  • 41

1 Answers1

0

I fixed it with the help of another stack question that someone helped me with.

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope

{

[self.filteredListContent removeAllObjects]; // First clear the filtered array.



for (XMLStringFile *new in rssOutputData_MutableArray)
{

    //Product scope
    if ([scope isEqualToString:@"Product"])
    {
        // Split into search text into separate "words"
        NSArray * searchComponents = [searchText componentsSeparatedByString: @" "];

        //Before searching trim off the whitespace
        searchText = [searchText stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];


        BOOL foundSearchText = YES;

        // Check each word to see if it was found
        for (NSString * searchComponent in searchComponents) {
            NSRange result = [new.xmlItem rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
            NSRange descriptionRange = [new.xmlDescription rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];

            foundSearchText &= (result.location != NSNotFound|| descriptionRange.location != NSNotFound);
        }

        // If all search words found, add to the array
        if (foundSearchText)
        {
            [self.filteredListContent addObject: new];
        }
    }

}

}

The winning line is:

//Before searching trim off the whitespace searchText = [searchText stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

Alex McPherson
  • 3,185
  • 3
  • 30
  • 41