我在我的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 Usenames 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);
}
}
}
任何人都可以提前帮助我.......提前!
答案 0 :(得分: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]];
}
}
答案 1 :(得分:1)
使用 rangeOfString 搜索整个字符串
for(NSString * strUserName in arrUserInfo )
{
NSRange range = [strUserName rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (range.length > 0)
{
[self.arrSearhResults addObject:strUserName];
}
}
答案 2 :(得分:0)
NSArray *array = [[NSArray alloc] initWithObjects:@"Ravi Kiran", @"sujay huilgol", nil];
for (NSString* name in array)
{
if ([name rangeOfString:@"Kiran"].location != NSNotFound)
NSLog(@"%@", name);
}