聯(lián)系人功能的需求一般都會有按照首字母排序,并且會要求同一個姓的就要連續(xù)起來中間不能穿插別的姓,百度了一下看到UILocalizedIndexedCollation給我們提供了很方便的排序方法,它不需要將中文轉(zhuǎn)為拼音,但是有一個缺點就是如果姓氏存在多音字就無法區(qū)分(例如:姓增,它會被分配到C (ceng)組)
下面貼代碼:
1,建一個類進行管理LinkManSort
.m文件
NSString *const CYPinyinGroupResultArray = @”CYPinyinGroupResultArray”;
NSString *const CYPinyinGroupCharArray = @”CYPinyinGroupCharArray”;
@implementation LinkManSort
// 按首字母分組排序數(shù)組 +(NSDictionary )sortObjectsAccordingToInitialWith:(NSArray )willSortArr SortKey:(NSString *)sortkey {
// 初始化UILocalizedIndexedCollation UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; //得出collation索引的數(shù)量,這里是27個(26個字母和1個#) NSInteger sectionTitlesCount = [[collation sectionTitles] count]; //初始化一個數(shù)組newSectionsArray用來存放最終的數(shù)據(jù),我們最終要得到的數(shù)據(jù)模型應(yīng)該形如@[@[以A開頭的數(shù)據(jù)數(shù)組], @[以B開頭的數(shù)據(jù)數(shù)組], @[以C開頭的數(shù)據(jù)數(shù)組], ... @[以#(其它)開頭的數(shù)據(jù)數(shù)組]] NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount]; //初始化27個空數(shù)組加入newSectionsArray for (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個數(shù)組中去 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中的數(shù)組按照name屬性排序 for (NSInteger index = 0; index < sectionTitlesCount; index++) { NSMutableArray *personArrayForSection = newSectionsArray[index]; NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)]; newSectionsArray[index] = sortedPersonArrayForSection; } //刪除空的數(shù)組 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};
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站aaarwkj.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁名稱:iOS實現(xiàn)聯(lián)系人按照首字母進行排序的實例-創(chuàng)新互聯(lián)
瀏覽路徑:http://aaarwkj.com/article40/dpeeeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、標簽優(yōu)化、用戶體驗、軟件開發(fā)、云服務(wù)器、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容