English 中文(简体)
正在搜索 UISearch Bar 中的第二个词
原标题:Searching for second word in UISearchBar

我用表格视图 + 搜索栏创建了一个视图。 如果在字符串中搜索第一个单词, 它将完全有效 。 不幸的是, 如果在字符串中搜索一个不同的单词, 它将无效 。 所以, 如果我搜索文本: bal 的话 。

我会有以下的绳子通过: 绿色的羽毛。它不会找到任何东西。

我目前正在比较搜索栏文本和数据 与这个代码:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{

[self.filteredListContent removeAllObjects]; // First clear the filtered array.

for (Product *product in self.listContent)
{

    NSComparisonResult result = [product.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
    if (result == NSOrderedSame)
    {
        [self.filteredListContent product];
    }
}
NSMutableSet *tempSet = [NSMutableSet setWithArray:self.filteredListContent];
self.filteredListContent = [NSMutableArray arrayWithArray:[tempSet allObjects]];
}

我怎样才能解决这个问题?

任何帮助或建议都将不胜感激!

最佳回答

本代码正在使用 compare: options: range: 方法在 NSString 中寻找精确匹配( 对案件和diacritics不敏感 ) :

NSComparisonResult result = [product.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
    [self.filteredListContent product];
}

鉴于您的问题描述, 您想要确定字符串是否包含在其它字符串中。 请使用 < code> rangeOf String: options: 方法在 < code> NSstring 中替换 : < code > 方法 : 方法 : < incode > NSstring :

NSRange range = [product.name rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (range.length > 0)
{
    [self.filteredListContent product];
}
问题回答

暂无回答




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签