聯系人功能的需求一般都會有按照首字母排序,并且會要求同一個姓的就要連續起來中間不能穿插別的姓,百度了一下看到UILocalizedIndexedCollation給我們提供了很方便的排序方法,它不需要將中文轉為拼音,但是有一個缺點就是如果姓氏存在多音字就無法區分(例如:姓增,它會被分配到C (ceng)組)
下面貼代碼:
1,建一個類進行管理LinkManSort
.m文件
NSString *const CYPinyinGroupResultArray = @”CYPinyinGroupResultArray”;
NSString *const CYPinyinGroupCharArray = @”CYPinyinGroupCharArray”;
@implementation LinkManSort
// 按首字母分組排序數組 +(NSDictionary )sortObjectsAccordingToInitialWith:(NSArray )willSortArr SortKey:(NSString *)sortkey {
// 初始化UILocalizedIndexedCollationUILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];//得出collation索引的數量,這里是27個(26個字母和1個#)NSInteger sectionTitlesCount = [[collation sectionTitles] count];//初始化一個數組newSectionsArray用來存放最終的數據,我們最終要得到的數據模型應該形如@[@[以A開頭的數據數組], @[以B開頭的數據數組], @[以C開頭的數據數組], ... @[以#(其它)開頭的數據數組]]NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];//初始化27個空數組加入newSectionsArrayfor (NSInteger index = 0; index < sectionTitlesCount; index++) { NSMutableArray *array = [[NSMutableArray alloc] init]; [newSectionsArray addObject:array];}NSLog(@"newSectionsArray %@ %@",newSectionsArray,collation.sectionTitles);NSMutableArray *firstChar = [NSMutableArray arrayWithCapacity:10];//將每個名字分到某個section下for (id Model in willSortArr) { //獲取name屬性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就為11 NSInteger sectionNumber = [collation sectionForObject:Model collationStringSelector:NSSelectorFromString(sortkey)]; //把name為“林丹”的p加入newSectionsArray中的第11個數組中去 NSMutableArray *sectionNames = newSectionsArray[sectionNumber]; [sectionNames addObject:Model]; NSString * str= collation.sectionTitles[sectionNumber]; [firstChar addObject:str]; NSLog(@"sectionNumbersectionNumber %ld %@",sectionNumber,str);}NSArray *firstCharResult = [self SortFirstChar:firstChar];NSLog(@"firstCharResult== %@",firstCharResult);//對每個section中的數組按照name屬性排序for (NSInteger index = 0; index < sectionTitlesCount; index++) { NSMutableArray *personArrayForSection = newSectionsArray[index]; NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)]; newSectionsArray[index] = sortedPersonArrayForSection;}//刪除空的數組NSMutableArray *finalArr = [NSMutableArray new];for (NSInteger index = 0; index < sectionTitlesCount; index++) { if (((NSMutableArray *)(newSectionsArray[index])).count != 0) { [finalArr addObject:newSectionsArray[index]]; }}return @{CYPinyinGroupResultArray:finalArr, CYPinyinGroupCharArray:firstCharResult};
}
+(NSArray )SortFirstChar:(NSArray )firstChararry{
//數組去重復NSMutableArray *noRepeat = [[NSMutableArray alloc]initWithCapacity:8];NSMutableSet *set = [[NSMutableSet alloc]initWithArray:firstChararry];[set enumerateObjectsUsingBlock:^(id obj , BOOL *stop){ [noRepeat addObject:obj];}];//字母排序NSArray *resultkArrSort1 = [noRepeat sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [obj1 compare:obj2 options:NSNumericSearch];}];//把”#“放在最后一位NSMutableArray *resultkArrSort2 = [[NSMutableArray alloc]initWithArray:resultkArrSort1];if ([resultkArrSort2 containsObject:@"#"]) { [resultkArrSort2 removeObject:@"#"]; [resultkArrSort2 addObject:@"#"];}return resultkArrSort2;
}
.h文件
先引入框架UIKit/UIKit.h /** * 獲取model數組 */ UIKIT_EXTERN NSString *const CYPinyinGroupResultArray;
/** * 獲取所包函字母的數組 */ UIKIT_EXTERN NSString *const CYPinyinGroupCharArray; @interface LinkManSort : NSObject +(NSDictionary )sortObjectsAccordingToInitialWith:(NSArray )willSortArr SortKey:(NSString *)sortkey ;
在VC里面調用
NSArray *arr = @[@{@”name”:@”李立”},@{@”name”:@” 李安”},@{@”name”:@”劉星”},@{@”name”:@”劉小米”},@{@”name”:@”蘇音”},@{@”name”:@”韋佳佳”},@{@”name”:@”李華”},@{@”name”:@”楊波”},@{@”name”:@”陳恒”},@{@”name”:@”黃呀呀”},@{@”name”:@”邱珀”},@{@”name”:@”李克”},@{@”name”:@”123456”},@{@”name”:@”韋立林”},@{@”name”:@”陳瑤”}];
NSMutableArray *marr = [NSMutableArray arrayWithCapacity:10];for (NSDictionary *dict in arr) { PersonModel *model =[[PersonModel alloc]init];// dict[@"name"]; model.name =dict[@"name"]; [marr addObject:model];}
NSDictionary *dcit= [LinkManSort sortObjectsAccordingToInitialWith:marr SortKey:@”name”];
NSArray *resultarr1 = dcit[CYPinyinGroupResultArray];//排好順序的PersonModel數組NSArray *resultarr2 = dcit[CYPinyinGroupCharArray];//排好順序的首字母數組
完成啦!
以上這篇iOS實現聯系人按照首字母進行排序的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答