0

以下のような商品を探しています。正常に動作しますが、2 つの単語間で検索すると結果が消え、ユーザーが次の単語の最初の文字を入力すると再び表示されます。つまり、「td pro」または「pro td」を検索すると、結果が表示されます。この td(i have a result) td(space) のように検索すると、結果はありません特定の順序で。もっと良い方法がない限り?

これが私のコードです:

- (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 に答える