需求點:程序員
常見通信錄基本功能,按首字母或者漢字拼音首字母分組排序索引。數組
須要解決的問題:this
一、本地化,世界語言那麼多,你都認識嗎?大部分人都是這樣的一種狀態吧:它認識我,可我不認識它啊。翻譯
二、以中文爲例,漢字轉換成拼音,而後再排序,想一想都頭疼。。code
三、暫時沒想到,槽點應該仍是有的,,吧。對象
解決思路:排序
秉持着,能用系統的,就不要本身寫的原則,盯着蘋果自家的通信錄,盯~~~,僞裝盯了很久(實際上是去找了下蘋果的API文檔),終於找到了它——UILocalizedIndexedCollation。索引
一、UILocalizedIndexedCollation的分組排序是創建在對象的操做上的。ip
二、主要的兩個API,以下,本身翻譯吧。ci
返回傳入object對象指定selector在[UILocalizedIndexedCollation currentCollation]中的匹配的索引 // Returns the index of the section that will contain the object. // selector must not take any arguments and return an NSString. - (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector;
經過上面的API篩選完後,將匹配某個索引的數組丟給array,並指定selector,按照selector對這個array進行再次排序,最終返回一個歸屬某個索引的按照首字母或漢字首字母排序的新數組。 // Used for sorting objects within the same section. // selector must not take any arguments and return an NSString. // In the process of sorting the array, each object may receive // selector multiple times, so this method should be fast. - (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector;
三、使用示例代碼
// 一、初始化一個索引,根據不一樣國家語言,會初始化出不一樣的索引,中文的是「A~Z,#」,供27個,其餘語言,本身試試吧。只看得懂中文的說。 UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; // 二、初始化一堆數據原對象,這裏省略一千萬個好友基本信息的對象模型(如:Person,person.name) // 三、獲取索引的數量,並初始化對應數量的空數組,用於存放篩選數據 NSInteger sectionTitlesCount = [[collation sectionTitles] count]; // 基本二維數組 NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount]; for (NSInteger index = 0; index < sectionTitlesCount; index++) { NSMutableArray *array = [[NSMutableArray alloc] init]; [newSectionsArray addObject:array]; } // 四、假設好友對象是Person,自帶屬性name for (Person *p in srcArray) { //獲取name屬性的值所在的位置,好比"小白鼠",首字母是X,在A~Z中排第23(第一位是0),sectionNumber就爲23 NSInteger sectionNumber = [collation sectionForObject:p collationStringSelector:@selector(name)]; //把name爲「小白鼠」的p加入newSectionsArray中的第23個數組中去 NSMutableArray *sectionNames = newSectionsArray[sectionNumber]; [sectionNames addObject:p]; } // 五、對每一個section中的數組按照name屬性排序 for (NSIntger i = 0; i < sectionTitlesCount; i++) { NSMutableArray *personArrayForSection = newSectionsArray[i]; NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)]; newSectionsArray[i] = sortedPersonArrayForSection; } // 六、最後能夠過濾下不存在數據的索引,省略
四、應用到一個tableview上
// 按照索引個數配置tableview區數 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [[UILocalizedIndexedCollation currentCollation] sectionTitles][section]; } // 配置索引內容,就是通信錄中右側的那一列「A~Z、#」 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]; } // 索引點擊響應 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index]; }
UILocalizedIndexedCollation沒法區分漢語中的多音字。畢竟中文博大精深嘛。