gpt4 book ai didi

iphone - 使用 iOS 中的 searchDisplay Controller 搜索单词或字符的任意组合

转载 作者:行者123 更新时间:2023-12-03 19:55:47 25 4
gpt4 key购买 nike

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

- (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 显示器的项目。我可以搜索 TD ProfessionalTD ProTD,无论大小写,它都会返回上述正确的项目。但是,如果我搜索Pro TD,它不会显示任何结果。我面临的问题是用户可能不知道产品标题或描述的顺序?所以我需要实现一些可以逻辑地做到这一点的东西。我真的不知道该做什么。

供引用

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

如有任何建议,我们将不胜感激。

最佳答案

如何在搜索字符串上使用 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];
}
}

关于iphone - 使用 iOS 中的 searchDisplay Controller 搜索单词或字符的任意组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10288897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com