最近在做公司的聊天項目,項目中涉及到了對自己的聯(lián)系人進行排序的功能,在網(wǎng)上查了下,大多說到了用蘋果自帶的UILocalizedIndexCollation
這個類吴藻,試了下很簡單,但是有個小問題弓柱,留在最后說沟堡,下面是大體的思路:
代碼如下:
#import "ViewController.h"
#import "QLContact.h"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *contactArray;
@property (nonatomic, strong) NSMutableArray *sortedArray;
@property (nonatomic, strong) UILocalizedIndexedCollation *collation;
@end
@implementation ViewController
- (NSMutableArray *)contactArray {
if (!_contactArray) {
_contactArray = [NSMutableArray array];
NSArray *firstNameArray = @[@"趙",@"錢",@"孫",@"李",@"周",@"吳",@"鄭",@"王",@"馮",@"陳",@"楮",@"衛(wèi)",@"蔣",@"沈",@"韓",@"楊",@"朱",@"秦",@"尤",@"許",@"何",@"呂",@"施",@"張",@"孔",@"曹",@"嚴",@"華",@"金",@"魏",@"陶",@"姜",@"戚",@"謝",@"鄒",@"喻",@"柏",@"水",@"竇",@"章",@"云",@"蘇",@"潘",@"葛",@"奚",@"范",@"彭",@"郎",@"魯",@"韋",@"昌",@"馬",@"苗",@"鳳",@"花",@"方",@"俞",@"任",@"袁",@"柳",@"酆",@"鮑",@"史",@"唐",@"費",@"廉",@"岑",@"薛",@"雷",@"賀",@"倪",@"湯",@"滕",@"殷",@"羅",@"畢",@"郝",@"鄔",@"安",@"常",@"樂",@"于",@"時",@"傅",@"皮",@"卞",@"齊",@"康",@"伍",@"余",@"元",@"卜",@"顧",@"孟",@"平",@"黃",@"和",@"穆",@"蕭",@"尹",@"姚",@"邵",@"湛",@"汪",@"祁",@"毛",@"禹",@"狄",@"米",@"貝",@"明",@"臧",@"計",@"伏",@"成",@"戴",@"談",@"宋",@"茅",@"龐",@"熊",@"紀",@"舒",@"屈",@"項",@"祝",@"董",@"梁",@"杜",@"阮",@"藍",@"閩",@"席",@"季",@"麻",@"強",@"賈",@"路",@"婁",@"危",@"江",@"童",@"顏",@"郭",@"梅",@"盛",@"林",@"刁",@"鍾",@"徐",@"丘",@"駱",@"高",@"夏",@"蔡",@"田",@"樊",@"胡",@"凌",@"霍",@"虞",@"萬",@"支",@"柯",@"昝",@"管",@"盧",@"莫",@"經(jīng)",@"房",@"裘",@"繆",@"干",@"解",@"應(yīng)",@"宗",@"丁",@"宣",@"賁",@"鄧",@"郁",@"單",@"杭",@"洪",@"包",@"諸",@"左",@"石",@"崔",@"吉",@"鈕",@"龔",@"程",@"嵇",@"邢",@"滑",@"裴",@"陸",@"榮",@"翁",@"荀",@"羊",@"於",@"惠",@"甄",@"麹",@"家",@"封",@"芮",@"羿",@"儲",@"靳",@"汲",@"邴",@"糜",@"松",@"井",@"段",@"富",@"巫",@"烏",@"焦",@"巴",@"弓",@"牧",@"隗",@"山",@"谷",@"車",@"侯",@"宓",@"蓬",@"全",@"郗",@"班",@"仰",@"秋",@"仲",@"伊",@"宮",@"寧",@"仇",@"欒",@"暴",@"甘",@"斜",@"厲",@"戎",@"祖",@"武",@"符",@"劉",@"景",@"詹",@"束",@"龍",@"葉",@"幸",@"司",@"韶",@"郜",@"黎",@"薊",@"薄",@"印",@"宿",@"白",@"懷",@"蒲",@"邰",@"從",@"鄂",@"索",@"咸",@"籍",@"賴",@"卓",@"藺",@"屠",@"蒙",@"池",@"喬",@"陰",@"郁",@"胥",@"能",@"蒼",@"雙",@"聞",@"莘",@"黨",@"翟",@"譚",@"貢",@"勞",@"逄",@"姬",@"申",@"扶",@"堵",@"冉",@"宰",@"酈",@"雍",@"郤",@"璩",@"桑",@"桂",@"濮",@"牛",@"壽",@"通",@"邊",@"扈",@"燕",@"冀",@"郟",@"浦",@"尚",@"農(nóng)",@"溫",@"別",@"莊",@"晏",@"柴",@"瞿",@"閻",@"充",@"慕",@"連",@"茹",@"習",@"宦",@"艾",@"魚",@"容",@"向",@"古",@"易",@"慎",@"戈",@"廖",@"庾",@"終",@"暨",@"居",@"衡",@"步",@"都",@"耿",@"滿",@"弘",@"匡",@"國",@"文",@"寇",@"廣",@"祿",@"闕",@"東",@"歐",@"殳",@"沃",@"利",@"蔚",@"越",@"夔",@"隆",@"師",@"鞏",@"厙",@"聶",@"晁",@"勾",@"敖",@"融",@"冷",@"訾",@"辛",@"闞",@"那",@"簡",@"饒",@"空",@"曾",@"毋",@"沙",@"乜",@"養(yǎng)",@"鞠",@"須",@"豐",@"巢",@"關(guān)",@"蒯",@"相",@"查",@"后",@"荊",@"紅",@"游",@"竺",@"權(quán)",@"逑",@"蓋",@"益",@"桓",@"公",@"萬俟",@"司馬",@"上官",@"歐陽",@"夏侯",@"諸葛",@"聞人",@"東方",@"赫連",@"皇甫",@"尉遲",@"公羊",@"澹臺",@"公冶",@"宗政",@"濮陽",@"淳于",@"單于",@"太叔",@"申屠",@"公孫",@"仲孫",@"軒轅",@"令狐",@"鍾離",@"宇文",@"長孫",@"慕容",@"鮮于",@"閭丘",@"司徒",@"司空",@"丌官",@"司寇",@"仉",@"督",@"子車",@"顓孫",@"端木",@"巫馬",@"公西",@"漆雕",@"樂正",@"壤駟",@"公良",@"拓拔",@"夾谷",@"宰父",@"谷梁",@"晉",@"楚",@"閻",@"法",@"汝",@"鄢",@"涂",@"欽",@"段干",@"百里",@"東郭",@"南門",@"呼延",@"歸",@"海",@"羊舌",@"微生",@"岳",@"帥",@"緱",@"亢",@"況",@"后",@"有",@"琴",@"梁丘",@"左丘",@"東門",@"西門",@"商",@"牟",@"佘",@"佴",@"伯",@"賞",@"南宮",@"墨",@"哈",@"譙",@"笪",@"年",@"愛",@"陽",@"佟"];
NSArray *secondNameArray = @[@"比",@"斯",@"靈",@"羅",@"里",@"佳",@"桐"];
for (NSString *firstName in firstNameArray) {
for (NSString *secondName in secondNameArray) {
QLContact *contact = [[QLContact alloc] init];
contact.name = [firstName stringByAppendingString:secondName];
[_contactArray addObject:contact];
}
}
}
return _contactArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.sortedArray = [NSMutableArray array];
self.collation = [UILocalizedIndexedCollation currentCollation];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
//創(chuàng)建存放相同首字母名字對象的數(shù)組
for (int i = 0; i < self.collation.sectionTitles.count; i++) {
NSMutableArray *mutArray = [NSMutableArray array];
[self.sortedArray addObject:mutArray];
}
//把各個聯(lián)系人對象取出來,放到對應(yīng)的數(shù)組中
for (QLContact *contact in self.contactArray) {
NSInteger position = [self.collation sectionForObject:contact collationStringSelector:@selector(name)];
[self.sortedArray[position] addObject:contact];
}
//再把存放著各個相同首字母名字對象的數(shù)組取出來矢空,將其中的聯(lián)系人對象按照名稱進行排序
[self.sortedArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSArray *array = [self.collation sortedArrayFromArray:obj collationStringSelector:@selector(name)];
self.sortedArray[idx] = array;
}];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.collation.sectionTitles.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.sortedArray[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
QLContact *contact = self.sortedArray[indexPath.section][indexPath.row];
cell.textLabel.text = contact.name;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([self.sortedArray[section] count]) {
return self.collation.sectionTitles[section];
} else {
return nil;
}
}
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.collation.sectionTitles;
}
@end
其中航罗,QLContact
是存儲聯(lián)系人信息的模型,有name
這個屬性屁药。
運行的結(jié)果如下:
如GIF所示粥血,存在的問題是當首字母出現(xiàn)同音字時,是按照名字的拼音順序排列酿箭,而不是按照同一個姓排在一起的規(guī)則排列
這個問題暫且擱在這里复亏,有更好的方法后再來修改。