1

我正在我的 iPhone 应用程序上进行搜索屏幕(搜索姓名列表),我需要同时按名字和姓氏进行搜索。

假设我的搜索表视图包含如下名称

Ravi Kiran 
sujay huilgol
harry potter 

我的搜索只对名字起作用,即如果我搜索Ra它会显示Ravi Kiran但是当我搜索Kiran时它不会显示Ravi Kiran

filterContentForSearchText的是这样的:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    [self.arrSearhResults removeAllObjects]; // First clear the filtered array.
    
    // Search the main list for Usernames whose type matches searchText; add items that match to the filtered array.
    
    for(NSString * strUserName in arrUserInfo )
    {
        NSComparisonResult result = [strUserName compare:searchText options:(NSLiteralSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
        if (result == NSOrderedSame)
        {
            // Update the filtered array based on the search text and scope.
            [self.arrSearhResults addObject:strUserName];
            //NSLog(@"arrSearhResults %@",arrSearhResults);
        }
    }
    
}

我可以尝试什么来解决这个问题?

4

3 回答 3

1

试试这个:

for(NSString * strUserName in arrUserInfo )
{
  NSRange titleResultsRange = [[NSString stringWithFormat:@"%@",strUserName] rangeOfString:searchText options:NSCaseInsensitiveSearch];
    if (titleResultsRange.length){
        [self.arrSearhResults addObject:[self.contacts objectAtIndex:i]];
    }
}
于 2012-09-28T06:16:03.137 回答
1

使用rangeOfString搜索整个字符串

 for(NSString * strUserName in arrUserInfo )
{
  NSRange range = [strUserName rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
  if (range.length > 0)
  {
    [self.arrSearhResults addObject:strUserName];
  }
}
于 2012-09-28T06:16:48.990 回答
0
NSArray *array = [[NSArray alloc] initWithObjects:@"Ravi Kiran", @"sujay huilgol", nil];
for (NSString* name in array)
{
    if ([name rangeOfString:@"Kiran"].location != NSNotFound)
        NSLog(@"%@", name);
}
于 2012-09-28T06:14:29.613 回答