2012-09-28 14 views
1

搜索我做一個搜索屏幕(搜索名單)在我的iPhone應用程序,在那裏我需要雙方的姓氏和名字如何通過雙方的名字和姓氏

說我的搜索搜索tableview中包含的名字,像如下

Ravi Kiran 
sujay huilgol 
harry potter 

我的搜索工作,即只有名字,如果我搜索它生病顯示拉維·基蘭但是當我搜索基蘭其不顯示鐳六基蘭

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); 
     } 
    } 

} 

可能有人幫助我,請..... thanx提前!

+0

NSOrderedSame如果接收器和ASTRING的字符串是詞彙值相等。 –

回答

1

使用rangeOfString搜索整個字符串

for(NSString * strUserName in arrUserInfo) 
{ 
    NSRange range = [strUserName rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]; 
    if (range.length > 0) 
    { 
    [self.arrSearhResults addObject:strUserName]; 
    } 
} 
0
NSArray *array = [[NSArray alloc] initWithObjects:@"Ravi Kiran", @"sujay huilgol", nil]; 
for (NSString* name in array) 
{ 
    if ([name rangeOfString:@"Kiran"].location != NSNotFound) 
     NSLog(@"%@", name); 
} 
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]]; 
    } 
}