嗯,獲取通訊錄還有問(wèn)題柿汛,使用靜態(tài)檢查發(fā)現(xiàn)有內(nèi)存泄漏的問(wèn)題就先不寫(xiě)了
先把排序加上去
//獲取字符串(或漢字)首字母
- (NSString *)firstCharacterWithString:(NSString *)string{
if (string.length == 0) {
return @"#";
}
NSMutableString *str = [NSMutableString stringWithString:string];
CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
NSString *pingyin = [str capitalizedString];
return [pingyin substringToIndex:1];
}
//將字符串?dāng)?shù)組按照元素首字母順序進(jìn)行排序分組
-
(NSMutableArray *)dictionaryOrderByCharacterWithOriginalArray:(NSArray *)array{
if (array.count == 0) {
return nil;
}
for (id obj in array) {
if (![obj isKindOfClass:[NSString class]]) {
return nil;
}
}
UILocalizedIndexedCollation * indexedCollation = [UILocalizedIndexedCollation currentCollation];
NSMutableArray *objects = [NSMutableArray arrayWithCapacity:indexedCollation.sectionTitles.count];
//UILocalizedIndexedCollation 系統(tǒng)的東西還是很強(qiáng)大的
for (int i = 0; i < indexedCollation.sectionTitles.count; i++) {
NSMutableArray *obj = [NSMutableArray array];
[objects addObject:obj];
}
//26個(gè)字母排序
for (int i = 0; i < array.count; i++) {
NSInteger index = [indexedCollation sectionForObject:array[i] collationStringSelector:@selector(uppercaseString)];
[[objects objectAtIndex:index] addObject:array[i]];}
return objects;
}
//漢字的拼音
-
(NSString *)pinyin{
NSMutableString *str = [self mutableCopy];
CFStringTransform(( CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);return [[str stringByReplacingOccurrencesOfString:@" " withString:@""] lowercaseString];
}
//然后就是把每組中的排序
NSMutableArray * nameArr = [NSString dictionaryOrderByCharacterWithOriginalArray:array];
for (NSArray * arr in nameArr) {
NSMutableArray * array = [[NSMutableArray alloc]initWithArray:arr];
NSInteger count = array.count;
for (int i = 0; i < count; i++) {
for (int j = 0; j < count - i - 1; j++) {
//這里我是把每個(gè)字符串都轉(zhuǎn)換成小寫(xiě)的拼音判斷的
if([[array[j] pinyin] compare:[array[j+1] pinyin] options:NSForcedOrderingSearch] == 1){
[array exchangeObjectAtIndex:j withObjectAtIndex:(j + 1)];
}
}
}
[_nameResultArr addObject:array];
}
PS:其實(shí)還有些沒(méi)解決冗酿,比如張和章 排序出來(lái)可能就是張、章络断、張裁替。。多音字也沒(méi)做貌笨,慢慢研究吧弱判。
好吧,一般這樣了 產(chǎn)品也會(huì)向微信學(xué)習(xí)加個(gè)搜索吧...
這種模糊搜索什么的 還是用UISearchController吧
代理中
-
(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSLog(@"%@",searchController.searchBar.text);
NSMutableArray * result = [[NSMutableArray alloc]init];
for (NSArray * arr in _nameResultArr) {
for (NSString * str in arr) {
//這里是先判斷輸入的內(nèi)容是否是漢字锥惋,如果是的話(huà)直接找昌腰,不是的話(huà)轉(zhuǎn)成拼音找
if ([NSString isHaveChineseInString:searchController.searchBar.text]) {
if ([str rangeOfString:searchController.searchBar.text].location != NSNotFound) {
[result addObject:str];
}
}else{
NSString * textStr = [searchController.searchBar.text lowercaseString];
if ([[str pinyin] rangeOfString:textStr].location != NSNotFound) {
[result addObject:str];
}
}}
}
[_searchController.searchResult setArray:result];[_searchController reloadSearchResult];
}
//判斷字符串中是否含有中文
- (BOOL)isHaveChineseInString:(NSString *)string{
for(NSInteger i = 0; i < [string length]; i++){
int a = [string characterAtIndex:i];
if (a > 0x4e00 && a < 0x9fff) {
return YES;
}
}
return NO;
}