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的全部内容。效果如图
iOS 名字按字母排序相关推荐
- 简单实现通讯录中文名字按拼音首字母排序
<body> <div class="address"> <div class="address_title">通讯录< ...
- 根据中文名字首字母进行分组
第一步:mysql 按中文名字首字母排序 第二步:循环列表,提取中文首字母 第三步:根据列表首字母进行分组 #mysql 按中文名字首字母排序 ORDER BY CONVERT ( 名字字段 US ...
- 联系人排序java代码_Android仿微信联系人按字母排序_脚本之家
App只要涉及到联系人的界面,几乎都是按照字母排序以及导航栏的方式.既然这个需求这么火,于是开始学习相关内容,此篇文章是我通过参考网上资料独立编写和总结的,希望多多少少对大家有所帮助,写的不好,还请各 ...
- android 通讯录字母排序,Android仿微信联系人字母排序效果
本文实例为大家分享了Android联系人字母排序的具体代码,供大家参考,具体内容如下 实现思路:首先说下布局,整个是一个相对布局,最下面是一个listview,listview上面是一个自定义的vie ...
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/12684155 前段时间因为换工作的缘故又恰巧碰到国庆节,所以有段时间自己没有更新博客了 ...
- 联系人排序java代码_Android仿微信联系人按字母排序
App只要涉及到联系人的界面,几乎都是按照字母排序以及导航栏的方式.既然这个需求这么火,于是开始学习相关内容,此篇文章是我通过参考网上资料独立编写和总结的,希望多多少少对大家有所帮助,写的不好,还请各 ...
- uni-app小程序,实现根据中文首字母排序功能
描述: 从后端调用接口获取所有热的姓名,将这些名字的首字母排序,然后放到对应字母下面,最终效果图如下: 实现过程 **总体实现的思路是:**首先调用接口,获取所有员工的姓名以及其他信息,将获取回来的中 ...
- 根据首字母排序英文名c语言,英文名按首字母排序的问题
2015-01-25 回答 nba截止2014-15赛季存在的球队英文名字按照首字母排序如下: atlanta hawks atl(亚特兰大老鹰) boston celtics cel(波士顿凯尔特人 ...
- iOS--中文排序、全拼音排序
本文示例Demo在GitHub:https://github.com/gaussli/ChineseSortDemo 在做项目的时候,总遇到排序问题,英文排序是很简单的,直接使用compare方法就可 ...
- android按字母数字排序,android 实现字母排序
//字母排序列表 String[] letter = { "A", "B", "C", "D", "E&quo ...
最新文章
- FDA转向AI加快药物研发和审批速度
- 复数 Complex Number 教程
- Android 多线程及线程通信
- TreeSet集合排序方式二:定制排序Comparator
- 从0开始利用宝塔linux面板+DSShop搭建一个简单的购物网站
- 交换机短路_你了解交换机的相关知识吗?还不赶快收藏起来
- 把代码push到github所遇到的问题
- 苹果可弯曲屏幕新专利获准,折叠iPhone最快2020年现身?
- 什么是Apple Desktop Bus? 亚行代表什么?
- barrons ap计算机科学,巴朗ap心理学barrons ap psychology, 5th edition-201-240.pdf
- 只要你上网,哪能不中毒?
- .net core精彩实例分享 -- 应用配置和数据库访问
- 小程序毕设作品之微信校园维修报修小程序毕业设计成品(7)中期检查报告
- 用 m3u8 下载网页视频直接保存为 MP4
- (摘之博客园狂奔di蜗牛)ASP.NET页面刷新方法总结
- 郑捷《机器学习算法原理与编程实践》学习笔记(第二章 中文文本分类(三)—KNN算法)...
- 人工智能研究中心快递柜——代码分析四
- java中的tld_自定义标签tld的使用
- latex 分页_latex 排版 首页不会换页
- 小米盒子4C刷机教程