【iOS】通讯录分组方式展示数据
本例子是将后台返回的医生列表(包含姓名和电话,demo从plist文件读取),按拼音进行分组显示(A-Z),最终效果如下图:
一、创建Doctor医生类:
Doctor类属性包括姓名、电话以及姓名第一个字的拼音首字母,同时生成初始化方法,对应的.h和.m文件如下:
JXDoctor.h
#import <Foundation/Foundation.h>@interface JXDoctor : NSObject
#pragma mark 姓名
@property (nonatomic,copy) NSString *name;
#pragma mark 姓名拼音索引
@property (nonatomic,strong) NSString *nameIndex;
#pragma mark 电话
@property (nonatomic,copy) NSString *phoneNum;#pragma mark 带参数的构造函数
-(JXDoctor *)initWithName:(NSString *)name andPhone:(NSString*)phone;#pragma mark 带参数的静态对象初始化方法
+(JXDoctor *)initWithName:(NSString *)name andPhone:(NSString*)phone;#pragma mark 根据字典初始化
+(instancetype)doctorWithDict:(NSDictionary*)dict;
-(instancetype)initWithDict:(NSDictionary*)dict;
@end
JXDoctor.m文件:
#import "JXDoctor.h"@implementation JXDoctor
-(JXDoctor *)initWithName:(NSString *)name andPhone:(NSString*)phone{if(self=[super init]){self.name=name;self.phoneNum=phone;self.nameIndex = [self pinyinFirstLetter:name];}return self;
}+(JXDoctor *)initWithName:(NSString *)name andPhone:(NSString*)phone{JXDoctor *doctor = [[JXDoctor alloc] initWithName:name andPhone:phone];return doctor;
}+(instancetype)doctorWithDict:(NSDictionary*)dict{return [[self alloc] initWithDict:dict];
}-(instancetype)initWithDict:(NSDictionary*)dict{if (self = [super init]) {//[self setValuesForKeysWithDictionary:dict];self.name = [dict objectForKey:@"name"];self.phoneNum = [dict objectForKey:@"phoneNum"];self.nameIndex = [self pinyinFirstLetter:[dict objectForKey:@"name"]];}return self;
}//获取字符串首字母
-(NSString*)pinyinFirstLetter:(NSString*) hanzi
{NSString *result = @"";NSMutableString *ms = [[NSMutableString alloc] initWithString:hanzi];if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {
// NSLog(@"pinyin1: %@", ms);}if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)){
// NSLog(@"pinyin2: %@", ms);}if (ms.length>0) {result = [ms substringToIndex:1];
// NSLog(@"pinyin3: %@", result);}return [result uppercaseString];
}
@end
二、从plist文件读取数据到doctorArr数组:
-(NSArray*)doctorArr{if (_doctorArr==nil) {NSArray *tempArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"doctors" ofType:@"plist"]];NSMutableArray *docArr = [NSMutableArray array];for (NSDictionary *dict in tempArr) {JXDoctor *doctor = [JXDoctor doctorWithDict:dict];[docArr addObject:doctor];}_doctorArr = docArr;}return _doctorArr;
}
*plist数据格式如下:
三、处理医生数据:
在ViewController中将从plist得到的数据转换为Doctor对象,并按照首字母进行分类,存放到
sections字典中。
-(void)setUpData{//创建所有的Keyssections = [NSMutableDictionary dictionary];BOOL found;for (JXDoctor *teamer in self.doctorArr) {NSString *index = teamer.nameIndex;found = NO;for(NSString *str in [sections allKeys]){if ([str isEqualToString:index]) {found = YES;}}//还没有对应的key,则新建if (!found) {NSLog(@"setValue:%@",index);[sections setValue:[[NSMutableArray alloc] init] forKey:index];}}//将所有Doctor数据加载进去for (JXDoctor *doctor in self.doctorArr){[[sections objectForKey:doctor.nameIndex] addObject:doctor];}//按A-Z排序for (NSString *key in [sections allKeys]){[[sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]];}NSLog(@"sections:%@",sections);[self.mainTableView reloadData];
}
四、实现UITableViewDataSource
#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return [[sections allKeys] count];
}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{NSLog(@"%d,%d",sections.count,[[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count]);return [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{return [[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *cellID = @"cellID";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];if (cell==nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];}JXDoctor *doctor = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];cell.textLabel.text = doctor.name;cell.detailTextLabel.text = doctor.phoneNum;return cell;
}//返回每组标题索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{return [[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
最后运行项目即可看到A—Z排序的效果, Demo下载:http://download.csdn.net/detail/dolacmeng/9291487
【iOS】通讯录分组方式展示数据相关推荐
- Xamarin.Forms读取并展示Android和iOS通讯录 - TerminalMACS客户端
本文同步更新地址: https://dotnet9.com/11520.html https://terminalmacs.com/861.html 阅读导航: 一.功能说明 二.代码实现 三.源码获 ...
- android 获取通讯录全选反选_Xamarin.Forms读取并展示Android和iOS通讯录 TerminalMACS客户端...
本文同步更新地址: https://dotnet9.com/11520.html https://terminalmacs.com/861.html 阅读导航: 一.功能说明 二.代码实现 三.源码获 ...
- iOS通讯录,蓝牙,内购等开发系列
–系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系 ...
- iOS 通讯录备份、恢复
和相册 备份一样,公司产品备份功能有通讯录备份 功能,自然也有通讯录恢复了.iOS通讯录相关操作,iOS对通讯录的操作离不开的框架: #import <AddressBook/A ...
- 4、Flutter - 控件基础 (二)ListView 列表展示数据、布局
ListView 列表展示数据.布局 接上篇,几点注意事项与说明 1.创建工程项目的时候,存放路径不要有中文,有中文会有警告,而且后面可能会有一些未知的问题. 如果有中文路径,创建完工程移动工程位置的 ...
- android ios通讯录权限设置,IOS 通讯录的访问和修改的实现
IOS 通讯录的访问和修改的实现 在iOS端可以通过AddressBook或者AddressBookUI两个框架实现,区别是第二个框架带视图,一般使用第一个框架就够了. 下面介绍AddressBook ...
- Android仿QQ通讯录分组展示ExpandableListView
Android仿QQ通讯录分组展示ExpandableListView 核心是重写BaseExpandableListAdpter,其实和之前写的普通的BaseAdapter是类似的, 但是BaseE ...
- layui做折线图_flask+layui+echarts实现前端动态图展示数据效果
效果图: 该效果主要实现一个table展示数据,并在下方生成一个折线图. 实现方式: 1.首先需要对表格进行一个数据加载,这里用到了layui的table.render,具体用法可以参考 https: ...
- ios realm 文件_iOS数据持久化方案-Realm的使用
总体内容 1.Realm介绍 2.使用教程与辅助工具 3.Realm的具体使用 一.Realm介绍 1.1.Realm 是一个跨平台移动数据库引擎,支持iOS.OS X(Objective-C和Swi ...
最新文章
- Nginx 的这些妙用,你都 get 到了吗?
- HAN:基于双层注意力机制的异质图深度神经网络
- 中文整合包_MIMOSA2: 基于微生物组和代谢组数据的整合分析
- Oracle导入程序Imp的使用详解
- rk修改launcher_RK launcher V 0.41 官方版
- 小游戏策划案例精选_小游戏策划方案
- 云在服务器上装系统,怎么在云服务器上安装系统
- 用摄动法证明fibs的一个公式(变形)
- win10u盘一直正在计算机,Windows10未插入U盘却一直显示的解决方法
- Sensible, not sensitive; simply complicated; predictably irrational; kindly demanding; constructivel
- js切换图片会闪动_js 图片闪动,间隔几分钟闪动一下
- 说你玻璃心的就想免费耍流氓
- 松香的用法(电烙铁焊接)
- A1033 To Fill or Not to Fill
- 亚稳态原因以及跨时钟处理方法
- Prometheus Operator 配置PrometheusRule告警规则
- 【二叉树】完美二叉树
- 记一次gitbook的安装
- Diffusion 扩散模型(DDPM)详解及torch复现
- PLC数据操作系列之构造不同应用场景的缓存栈FIFO(算法详解)
热门文章
- Activiti 规则任务(businessRuleTask)
- 蓝桥杯-搭积木-java
- Unity应用架构设计(9)——构建统一的 Repository
- Creating Apps With Material Design —— Defining Custom Animations
- window resize和scroll事件的基本优化
- 【字符串操作之】返回指定位置的字符和Unicode 字符代码 根据unicode返回字符→→charAt、charCodeAt和fromCharCode...
- 如何查询并解决80端口 (转)
- Squid如何提高命中率
- 在 C# 中通过 P/Invoke 调用Win32 DLL
- 将 SQL Server 存储过程用于数据访问