iOS 名字按字母排序

由于项目需要制作一个通讯录,于是就研究了一下,不足之处还请指教:

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSMutableDictionary *nameDic;
@property (nonatomic,strong) NSMutableDictionary *numdic;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *names = @[@"张三",@"李四",@"王麻子",@"王五",@"刘备",@"张飞",@"关羽",@"刘玉婵",@"吕布",@"诸葛老神仙",@"陈二狗",@"曹蒹葭",@"白马探花"];
    NSArray *nums = @[@"张三1",@"李四2",@"王麻子3",@"王五4",@"刘备5",@"张飞6",@"关羽7",@"刘玉婵4",@"吕布8",@"诸葛老神仙8",@"陈二狗3",@"曹蒹葭2",@"白马探花1"];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    for ( int i = 0; i<names.count; i++) {
        [dic setValue:nums[i] forKey:names[i]];
    }
    self.numdic = dic;
    UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
   
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view  addSubview:tableView];
    for (NSString *name in names) {
        //获取汉子的首字母
        //把中文转拼音
     NSMutableString *ms = [[NSMutableString alloc] initWithString:name];
        if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {
                     NSLog(@"Pingying: %@", ms);
        }
        if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
                      NSLog(@"Pingying: %@", ms);
        }
        NSString *firstString = [ms substringToIndex:1];
        
        //        NSLog(@"%@--%@--%@",cityName,ms,[ms substringToIndex:1]);
        
        NSMutableArray * arr = [self.nameDic objectForKey:firstString];
        if (arr) {
            [arr addObject:name];
        }else{//如果没有 则需要创建一个添加到里面
            [self.nameDic setObject:[@[name] mutableCopy] forKey:firstString];
        }
        
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        
    return self.nameDic.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //对数组中的字符串进行排序
    NSArray *keys = [self.nameDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    NSString *key = keys[section];
    
    NSArray *citys = self.nameDic[key];
    
    return citys.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *keys = [self.nameDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    NSString *key = keys[indexPath.section];
    
    NSArray *citys = self.nameDic[key];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text =  [NSString stringWithFormat:@"名字:%@ \n 电话:%@ ", citys[indexPath.row],self.numdic[citys[indexPath.row]]];
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
//控制有没有索引
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    
    NSArray *keys = [self.nameDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
    return keys;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSArray *keys = [self.nameDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    return keys[section];
}
-(NSMutableDictionary *)nameDic{
    if (_nameDic == nil) {
        _nameDic = [NSMutableDictionary dictionary];
    }
    return _nameDic;
}
@end
 

 

这就是我所写demo的全部内容。效果如图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值