0

我正在搜索我的产品,如下所示。它工作正常,但是当在 2 个单词之间搜索时,结果会消失,然后在用户输入下一个单词的第一个字符后重新出现。即,如果我搜索“td pro”或“pro td”,结果就在那里。如果我像这样搜索 td(i have a result) td(space) I have NO result td p(I have a result) 只是为了添加我需要在 componentsSeperatedByString 中用空格分隔单词,因为用户可能不会搜索产品以任何特定的顺序。除非有更好的方法?

这是我的代码:

- (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];
        }
    }

}

}

非常感谢

4

1 回答 1

0

我在有人帮助我解决的另一个堆栈问题的帮助下修复了它。

- (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];
        }
    }

}

}

获胜线是:

//搜索前修剪掉空格 searchText = [searchText stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

于 2012-06-01T13:54:40.040 回答