首先生成索引字典
-
其中所有key值即为分区的大写字母
-
value值为一个个数组,每个分区的数据
#pragma mark --- 生成索引字典
-(NSDictionary *) indexDictionaryBySortArray:(NSMutableArray *)marray withObjectKey:(NSString *)akey
{
// [self sortArray:marray withObjectKey:akey];
QiLa_FamousEditFrameSet *frameSet = nil;
NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
if ([akey isEqualToString:@"nickName"])
{
for (int i=0; i<[marray count]; i++)
{
frameSet = [marray objectAtIndex:i];
NSString * first = nil;
NSString * str = @"^[A-Za-z]+.*[\u4e00-\u9fa5]*";
NSPredicate * pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
if ([pre evaluateWithObject:frameSet.famousModel.nickName]) {
first = [[frameSet.famousModel.nickName substringToIndex:1] uppercaseString];
}else{
NSString * abcStr = [NSString stringWithFormat:@"%@",frameSet.famousModel.nickName];
NSString * str1 = @"^[\u4e00-\u9fa5]+.*[A-Za-z]*";
NSPredicate * pre1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",str1];
if (![abcStr isEqualToString:@""] && ![abcStr isKindOfClass:[NSNull class]] && abcStr) {
if ([pre1 evaluateWithObject:abcStr]) {
unichar firstChar = [abcStr characterAtIndex:0];
first = [[NSString stringWithFormat:@"%c", pinyinFirstLetter((unsigned short)firstChar)] uppercaseString];
}else {
first = @"#";
}
}else{
first = @"#";
}
}
if (!dic) {
NSMutableArray *arr = [NSMutableArray array];
[arr addObject:frameSet];
[dic setObject:arr forKey:first];
}else if ([[dic allKeys] containsObject:first]){
[[dic objectForKey:first] addObject:frameSet];
}else{
NSMutableArray * arr = [NSMutableArray array];
[arr addObject:frameSet];
[dic setObject:arr forKey:first];
}
}
}
return dic;
}
对所有的key值排序
[self.sectionTitles sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSString * s1 = (NSString *) obj1;
NSString * s2 = (NSString *) obj2;
return [s1 localizedCompare:s2];
}];
self.sectionTitles是一个排完序之后的数组,也就是数据源,给tableView所有代理赋值
网友评论