如何使搜索栏成为iPhone联系人应用程序或搜索栏,处理多个不完整的kewords

时间:2017-02-10 10:22:59

标签: ios objective-c uisearchbar predicate

我创建了一个搜索栏,其行为应该像iPhone联系人搜索栏。目前它的确有部分功能。  这是我的代码

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
     NSRange whiteSpaceRange = [result rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
        if (whiteSpaceRange.location != NSNotFound)
        {
            NSString * stringAfterSpace = [result substringFromIndex:whiteSpaceRange.location];
            NSString * stringBeforeSpace= [result substringToIndex:whiteSpaceRange.location];

            NSPredicate *predicateForAfterSpace=[NSPredicate predicateWithFormat:@"catNm CONTAINS[cd] %@",stringAfterSpace];

            NSPredicate *predicateForBegining = [NSPredicate predicateWithFormat:@"catNm BEGINSWITH[cd] %@", stringBeforeSpace];

            NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicateForBegining, predicateForAfterSpace]];
            searchArray = [[dataArray filteredArrayUsingPredicate:compoundPredicate] mutableCopy];

        }
        else
        {
            NSPredicate *predicateForBegining = [NSPredicate predicateWithFormat:@"catNm BEGINSWITH[cd] %@", result];

            NSString *stringAfterSpace=[NSString stringWithFormat:@" %@",result];
            NSPredicate *predicateForAfterSpace=[NSPredicate predicateWithFormat:@"catNm CONTAINS[cd] %@",stringAfterSpace];

            NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicateForBegining,predicateForAfterSpace]];
            searchArray = [[dataArray filteredArrayUsingPredicate:compoundPredicate] mutableCopy];
        }
}

主要区别在于我的searchBar处理一个不完整的关键字,但不超过一个。假设array={{catNm=@"Asus Zenfone 3 Max"},{catNm=@"Asus Zenfone Go}} 如果我搜索asu zenzen ma,则会显示结果,但如果搜索asus zen max则会出错 我想知道的是如何处理searchBar中的多个空格

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容:正则表达式(正则表达式)。

NSString *keyToFilter = @"catNm";

NSArray *dataArray = @[@{keyToFilter: @"Asus Zenfone 3 Max"},
                     @{keyToFilter: @"Asus Zenfone Go"}];

NSArray *wordsSearched = [searchString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

NSMutableString *pattern = [[NSMutableString alloc] initWithString:@".*\\b"];
[pattern appendString:[wordsSearched componentsJoinedByString:@".*\\b"]];
[pattern appendString:@".*"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.%K MATCHES[C] %@", keyToFilter, pattern];
NSArray *filtered = [dataArray filteredArrayUsingPredicate:predicate];
NSLog(@"SearchString: %@\nFiltered: %@", searchString, ([filtered count]?[filtered valueForKey:keyToFilter]:@"NO RESULT"));

我对正则表达式不太自信,但应该这样做。如果你想添加更复杂的东西,你可以使用在线RegexTools。

结果/样品测试:

$>SearchString: "asus zen max"
Filtered: (
    "Asus Zenfone 3 Max"
)
$>SearchString: "zen ma"
Filtered: (
    "Asus Zenfone 3 Max"
)
$>SearchString: "asu zen"
Filtered: (
    "Asus Zenfone 3 Max",
    "Asus Zenfone Go"
)
$>SearchString: "zen"
Filtered: (
    "Asus Zenfone 3 Max",
    "Asus Zenfone Go"
)
$>SearchString: "M"
Filtered: (
    "Asus Zenfone 3 Max"
)
$>SearchString: "sams"
Filtered: NO RESULT
$>SearchString: "zen asu"
Filtered: NO RESULT
$>SearchString: "zen g"
Filtered: (
    "Asus Zenfone Go"
)
$>SearchString: "a   g"
Filtered: (
    "Asus Zenfone Go"
)