iOS 拼音排序

释放双眼,带上耳机,听听看~!
//模拟数据
-(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;
}

如果需要分组的话

  1. 分组数量 _dataArr.count
  2. 分区中Cell 个数 [_dataArr[section][@”info”]count]
  3. 添加索引
    //添加索引列
    -(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;
    }
    

     

人已赞赏
iOS文章

iOS UITextFile手机号码限制11位和粘贴冲突解决办法

2019-12-5 14:18:24

iOS文章

iOS开发引入c++文件报错ld: symbol(s) not found for architecture arm64

2019-12-5 14:50:29

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索