//模拟数据
-(void)loadData
{
NSArray *dataArr = @[
@"比昂",@"重庆",@"重要",@"小明",@"张三",@"秋秋",@"波比",@"苏三",@"馒头",@"彩笔",@"咪咪",@"蔡芯",@"成龙",@"陈鑫",@"陈丹",@"成名",@"阿伟",@"阿姨",@"芳仔",@"房祖名",@"方大同",@"芳芳",@"范伟",@"郭靖",@"郭美美",@"过儿",@"过山车"
];
NSLog(@"原始数据 ==>> %@",dataArr);
//yes 分组no 不分组只把数据排序
_dataArr = [self Transformation:dataArr isGrouping:NO];
NSLog(@"数据转换 ==>> %@",_dataArr);
[self.aTableView reloadData];
}- (NSMutableArray *)Transformation:(NSArray *)dataArray isGrouping:(BOOL)grouping
{
NSMutableArray *TwoArr = [NSMutableArray array];
for (NSString *TransformationStr in dataArray)
{
NSMutableString *pinyin = [TransformationStr mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
[TwoArr addObject:pinyin];
}
NSMutableArray *ThreeArr = [NSMutableArray array];
for (__strong NSString *ScreenStr in TwoArr)
{
ScreenStr = [ScreenStr substringToIndex:1];
if (ThreeArr.count == 0)
{
[ThreeArr addObject:ScreenStr];
}
else
{
BOOL bl = [ThreeArr containsObject:ScreenStr];
if (!bl)
{
[ThreeArr addObject:ScreenStr];
}
}
}
NSArray *FourArr = [ThreeArr sortedArrayUsingSelector:@selector(compare:)];
NSMutableArray *MyArr = [NSMutableArray array];
NSMutableArray *allNewDataArr = [NSMutableArray array];
for (NSString *str in FourArr)
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:str forKey:@"title"];
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0;
i < TwoArr.count;
i++)
{
NSString *missstr = TwoArr[i];
missstr = [missstr substringToIndex:1];
if ([str isEqualToString:missstr])
{
[arr addObject:dataArray[i]];
}
}
[dic setObject:arr forKey:@"info"];
for (id obj in arr) {
[allNewDataArr addObject:obj];
}[MyArr addObject:dic];
}
return grouping ? MyArr : allNewDataArr;
}
【iOS|iOS 拼音排序】如果需要分组的话
- 分组数量 _dataArr.count
- 分区中Cell 个数 [_dataArr[section][@"info"]count]
- 添加索引
//添加索引列
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *arr = [NSMutableArray array];
for (NSDictionary *dic in _dataArr) {
[arr addObject:dic[@"title"]];
}
return arr;
}//索引列点击事件
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
[tableView
scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]
atScrollPosition:UITableViewScrollPositionTop animated:YES];
return index;
}