【问题标题】:Search any combination of words or characters using searchDisplay controller in iOS在 iOS 中使用 searchDisplay 控制器搜索单词或字符的任意组合
【发布时间】:2012-04-25 17:22:54
【问题描述】:

您好,我有一个填充 tableview 的主数组,并且我有一个用于搜索结果的过滤数组。使用以下方法,这两个都可以正常工作。除了此代码块下的以下问题外,我的 tableview 和搜索显示数组运行良好。

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

}

我想要实现的是这个。我在主阵列中有一个名为 LMD-2451 TD Professional 3D LCD Monitor 的项目。我可以搜索 TD ProfessionalTD ProTD 并使用任何大小写返回上述正确的项目。但是,如果我搜索 Pro TD,它不会显示任何结果。我面临的问题是用户可能不知道产品标题或描述的顺序?所以我需要实现一些可以在逻辑上做到这一点的东西。我真的不知道该怎么做。

供参考

NSMutableArray *parserData; // 完成主完整数组 NSMutableArray *filteredListContent; // 搜索结果数组

任何建议将不胜感激。

【问题讨论】:

    标签: iphone ios uitableview uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    如何在您的搜索字符串上使用 componentsSeperatedByString:@" " 将其拆分为(在您的示例中)一个由 2 个字符串组成的数组,@"Pro"@"TD"。然后使用rangeOfString: 循环遍历数组,检查数组中的所有组件是否在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];
        }
    }
    

    【讨论】:

    • 抱歉,Ashley 我明白你的概念,但我正在努力弄清楚如何实现这一点。我首先需要拆分 parserData 数组吗?你能举个简单的例子吗?
    • 谁能进一步帮助我解决这个问题?
    • 更新了我的答案,抱歉耽搁了!
    • 阿什利,你就是男人!当你知道如何时,非常感谢似乎真的很明显。我只需要更改一个部分就可以了,那就是将 rangeOfString:searchText 替换为 rangeOfString:searchComponent.. 一切都很好 如果你把你的电子邮件地址发给我,我非常感谢你,我很乐意给你寄一个小礼物你的帮助! :-)
    • @AlexMcPherson - 你的感谢就足够了。很高兴能够提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多