我正在玩Apple的TableSearch示例应用程序.
在他们的应用程序中,他们有一个Apple产品阵列.有一排"iPod touch".搜索"触摸"时,不显示任何结果.
有人可以帮助我使每行中的所有单词都可搜索吗?因此,搜索"iPod"时会找到结果,但搜索关键字"touch"时也会找到结果.
干杯.
tit*_*coy 14
下面是filterContentForSearchText:scope:MainViewController.m 中方法的相关代码:
NSComparisonResult result = [product.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:product];
}Run Code Online (Sandbox Code Playgroud)
这将比较前n个字符(由range参数指定),忽略大小写和变音符号,每个字符串与当前搜索字符串的前n个字符,其中n是当前搜索字符串的长度.
尝试将代码更改为以下内容:
NSRange result = [product.name rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (result.location != NSNotFound)
{
[self.filteredListContent addObject:product];
}Run Code Online (Sandbox Code Playgroud)
这将在每个字符串中搜索当前搜索字符串.
| 归档时间: |
|
| 查看次数: |
2374 次 |
| 最近记录: |