【iOS】通讯录分组方式展示数据
来源:互联网 发布:南国英雄传 知乎 编辑:程序博客网 时间:2024/05/20 17:39
本例子是将后台返回的医生列表(包含姓名和电话,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{ //创建所有的Keys sections = [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
0 0
- 【iOS】通讯录分组方式展示数据
- iOS程序-UITableView分组展示数据
- Android仿QQ通讯录分组展示ExpandableListView
- iOS程序-UITableView分组展示数据 - 纯手写代码
- iOS 获取通讯录联系人信息并展示
- iOS UItableView通讯录(分组显示)
- ios 通讯录 获取分组以及各组联系人
- iOS开发------通讯录分组排序(UILocalizedIndexedCollation)
- 使用ExpandableListView+GridView实现分组数据展示
- ios中访问通讯录数据
- iOS提取本地通讯录数据
- iOS 获取通讯录的数据
- iOS 中文首字母排序,通讯录排序,好友分组排序
- iOS 中文首字母排序,通讯录排序,好友分组排序
- iOS分组通讯录效果+侧滑菜单(MMDrawerController)
- Android通讯录分组管理
- 通讯录分组排序(UILocalizedIndexedCollation)
- iphone开发之表格组件UITableView的使用(三)通过加载plist文件字典转模型方式展示分组数据
- spring的依赖注入是什么意思
- APK安装详解
- 关于艾宾浩斯曲线的一点思考
- java内部类
- 适配器模式(结构型)
- 【iOS】通讯录分组方式展示数据
- 单例模式(创建型)
- hiho刷题日记——第十八天RMQ问题再临
- C++中endl的本质是什么
- cocos定时器
- web开发--- 牛腩总:细节篇
- (一)Activity间通信对象---------------Intent
- 树莓派入门之装系统
- 23种设计模式与6大原则综述