实现通讯录功能-使用单例封装写法
来源:互联网 发布:vb读取excel单元格 编辑:程序博客网 时间:2024/06/06 03:31
功能实现:
纯代码实现通讯录的增删移动和修改等功能,在删除增加修改的同时能同时以首字母为分组....
创建两个视图控制器,修改之前联系人详情 或者 添加联系人 公用第二个视图控制器
有需要交流或者需要原工程代码的请加Q291975388
界面时间问题, 布局的可能不是很美观. 但这都不是重点
上代码
//// RootViewController.m// AddressBook//// Created by lanou on 15/11/25.// Copyright (c) 2015年 ZhangFengtian. All rights reserved.//#import "RootViewController.h"#import "AddressBookHandle.h"#import "SecondViewController.h"@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self addTableView]; [self addBarButtonItem]; self.navigationItem.title = @"通讯录";}// 添加编辑按钮- (void)addBarButtonItem{ UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClick:)]; self.navigationItem.rightBarButtonItem = rightButton; [rightButton release]; UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"添加联系人" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClick:)]; self.navigationItem.leftBarButtonItem = leftButton; [leftButton release];}// 编辑按钮的方法- (void)rightButtonClick:(UIBarButtonItem *)rightButtom{ // 开启编辑状态 UITableView *tableView = (UITableView *)[self.view viewWithTag:100]; [tableView setEditing:!tableView.editing animated:YES]; if (tableView.editing == YES) { rightButtom.title = @"完成"; }else{ rightButtom.title = @"编辑"; }}- (void)leftButtonClick:(UIBarButtonItem *)rightButtom{ SecondViewController *secondVC = [[[SecondViewController alloc] init] autorelease]; UITableView *tableView = (UITableView *)[self.view viewWithTag:100]; secondVC.tbview = tableView; // 直接模态过去是没有导航栏的 UINavigationController *navC = [[[UINavigationController alloc] initWithRootViewController:secondVC] autorelease]; [self presentViewController:navC animated:YES completion:nil];}// 创建tableView- (void)addTableView{ UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)]; tableView.dataSource = self; tableView.delegate = self; tableView.tag = 100; [self.view addSubview:tableView]; [tableView release];}// 返回分区- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [[AddressBookHandle shareHandle] numberOfSection];}// 返回每个分区的行数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // 获取单例对象 用对象去调用方法 return [[AddressBookHandle shareHandle] numberOfRowsInSection:section];}// 创建cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identfier = @"myCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identfier] autorelease]; } // 显示数据 ContactModel *model = [[AddressBookHandle shareHandle] cellModelAtIndexPath:indexPath]; cell.textLabel.text = model.name; cell.detailTextLabel.text = model.phoneNumber; cell.imageView.image = [UIImage imageNamed:model.picture]; return cell;}// 返回分组名- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [[AddressBookHandle shareHandle] titleForCellHeaderInSection:section];}// 返回侧边按钮- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return [[AddressBookHandle shareHandle] rightButtonForCell];}// 点击cell调到下一个界面- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ SecondViewController *secondVC = [[[SecondViewController alloc] init] autorelease]; secondVC.model = [[AddressBookHandle shareHandle] cellModelAtIndexPath:indexPath]; secondVC.tbview = tableView; [self.navigationController pushViewController:secondVC animated:YES];}#pragma mark -- 编辑// 允许编辑- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}// 设置编辑样式- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete;}// 完成提交编辑- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ [[AddressBookHandle shareHandle] tableView:tableView forRowAtIndexPath:indexPath];}#pragma mark -- 移动// 允许移动- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}// 提交移动- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ [[AddressBookHandle shareHandle] tableView:tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];}// 限制跨区移动- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{ if (sourceIndexPath.section == proposedDestinationIndexPath.section) { return proposedDestinationIndexPath; }else{ return sourceIndexPath; }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
//// SecondViewController.h// AddressBook//// Created by lanou on 15/11/25.// Copyright (c) 2015年 ZhangFengtian. All rights reserved.//#import <UIKit/UIKit.h>#import "ContactModel.h"@interface SecondViewController : UIViewController<UITextFieldDelegate>@property(nonatomic ,retain)ContactModel *model;@property(nonatomic ,retain)UITableView *tbview;@end
//// SecondViewController.m// AddressBook//// Created by lanou on 15/11/25.// Copyright (c) 2015年 ZhangFengtian. All rights reserved.//#import "SecondViewController.h"#import "StudentView.h"#import "AddressBookHandle.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)dealloc{ [_model release]; [super dealloc];}- (void)loadView{ StudentView *studentView = [[StudentView alloc] init]; self.view = studentView; studentView.tag = 100; [studentView release];}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"详情"; [self setUpData]; [self addBarButtonItem]; }- (void)setUpData{ StudentView *studentView = (StudentView *)[self.view viewWithTag:100]; studentView.nameTF.text = self.model.name; studentView.phoneNumberTF.text = self.model.phoneNumber; studentView.sexTF.text = self.model.gender; studentView.hobbyTF.text = self.model.hobby; studentView.pictureTF.text = self.model.picture; studentView.ageTF.text = self.model.age; studentView.ageTF.delegate = self; studentView.nameTF.delegate = self; studentView.phoneNumberTF.delegate = self; studentView.sexTF.delegate = self; studentView.hobbyTF.delegate = self; studentView.pictureTF.delegate = self;}// 添加保存按钮- (void)addBarButtonItem{ UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClick:)]; self.navigationItem.rightBarButtonItem = rightButton; [rightButton release]; // 添加返回按钮 UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftButtonClick:)]; self.navigationItem.leftBarButtonItem = leftButton; [leftButton release]; }// 实现保存的方法- (void)rightButtonClick:(UIBarButtonItem *)rightButtom{ StudentView *studentView = (StudentView *)[self.view viewWithTag:100]; // 如果名字为空 if (studentView.nameTF.text.length == 0 || [studentView.nameTF.text isEqualToString:@" "] || studentView.phoneNumberTF.text.length == 0 || [studentView.phoneNumberTF.text isEqualToString:@" "]) { NSLog(@"名字号码不能为空"); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"名字和号码不能为空" message:nil delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil]; [alertView show]; [alertView release]; }else{ [self changeData]; [self popViewController]; }}- (void)changeData{ StudentView *studentView = (StudentView *)[self.view viewWithTag:100]; ContactModel *newModel = [[[ContactModel alloc] init] autorelease]; newModel.name = studentView.nameTF.text; newModel.gender = studentView.sexTF.text; newModel.phoneNumber = studentView.phoneNumberTF.text; newModel.hobby = studentView.hobbyTF.text; newModel.picture = studentView.pictureTF.text; newModel.age = studentView.ageTF.text; if (self.model == nil) { [[AddressBookHandle shareHandle] addNewModel:newModel tableView:self.tbview]; } else{ [[AddressBookHandle shareHandle] changeData:self.model newModel:newModel tabelView:self.tbview]; } }// 实现返回按钮- (void)leftButtonClick:(UIBarButtonItem *)rightButtom{ [self popViewController];}// 返回方法- (void)popViewController{ // 利用model的值是否为空来判断是模态进入的还是push进入的 if (self.model == nil) { // 模态进来 [self dismissViewControllerAnimated:YES completion:nil]; }else { // push 进来 [self.navigationController popViewControllerAnimated:YES]; }}- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
创建单例类 AddressBookHandle
.h
//// AddressBookHandle.h// AddressBook//// Created by lanou on 15/11/25.// Copyright (c) 2015年 ZhangFengtian. All rights reserved.//#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>#import "ContactModel.h"@interface AddressBookHandle : NSObject// 单例对象的初始化方法+ (AddressBookHandle *)shareHandle;// 保存联系人的字典// 保存排序完成的key的数组@property(nonatomic, retain)NSMutableDictionary *dataDic;@property(nonatomic, retain)NSMutableArray *sortKeysArray;// 返回分区数的方法- (NSInteger)numberOfSection;// 返回每个分区有多少行- (NSInteger)numberOfRowsInSection:(NSInteger)section;// 根据索引返回字典中的model// NSIndexPath在Foundation框架下没有row和section的属性// 这两个属性在UIKit框架下的UITableView的头文件下(NSIndexPath的类目)// 所以要使用row和section这两个属性 必须要引入UIKit框架- (ContactModel *)cellModelAtIndexPath:(NSIndexPath *)indexPath;// 返回分区表头- (NSString *)titleForCellHeaderInSection:(NSInteger)section;// 返回侧边按钮- (NSArray *)rightButtonForCell;// 提交编辑- (void)tableView:(UITableView *)tableView forRowAtIndexPath:(NSIndexPath *)indexPath;// 提交移动- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;// 修改数据- (void)changeData:(ContactModel *)model newModel:(ContactModel *)newModel tabelView:(UITableView *)tableView;// 添加数据- (void)addNewModel:(ContactModel *)newModel tableView:(UITableView *)tableView;@end.m
//// AddressBookHandle.m// AddressBook//// Created by lanou on 15/11/25.// Copyright (c) 2015年 ZhangFengtian. All rights reserved.//#import "AddressBookHandle.h"#import "NSString+Characters.h"@implementation AddressBookHandle- (void)dealloc{ [_dataDic release]; [_sortKeysArray release]; [super dealloc];}+ (AddressBookHandle *)shareHandle{ static AddressBookHandle *handle = nil; if (handle == nil) { handle = [[AddressBookHandle alloc] init]; } return handle;}// 重写初始化方法在初始化的同时 加载数据- (instancetype)init{ self = [super init]; if (self) { // 调用加载数据 [self setUpData]; } return self;}// 获取数据- (void)setUpData{ // 获取路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"Class14ContactList" ofType:@"plist"]; // 从路径读取数据 NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path]; // 进行剥皮(字典转模型 放进字典中) // 取出所有key NSArray *allkeys = dic.allKeys; // 遍历字典 self.dataDic = [NSMutableDictionary dictionary]; for (int i = 0;i < dic.count; i++) { NSString *key = allkeys[i]; NSArray *values = dic[key]; // 遍历数组 NSMutableArray *newValues = [NSMutableArray array]; for (NSDictionary *oneDic in values) { ContactModel *model = [[ContactModel alloc] init]; // KVC赋值 [model setValuesForKeysWithDictionary:oneDic]; // 添加临时数组 [newValues addObject:model]; [model release]; } // 添加进新字典 [self.dataDic setValue:newValues forKey:key]; } // 对key进行排序 self.sortKeysArray = [NSMutableArray arrayWithArray:[allkeys sortedArrayUsingSelector:@selector(compare:)]]; }// 修改数据- (void)changeData:(ContactModel *)model newModel:(ContactModel *)newModel tabelView:(UITableView *)tableView;{ // 先把新的model加入字典和数组中 [self addNewModel:newModel tableView:tableView]; // 判断删除旧的model NSString *groupName = [[model.name pinyinOfName] firstCharacterOfName]; NSMutableArray *value = [self.dataDic objectForKey:groupName]; [value removeObject:model]; if (value.count == 0) { [self.dataDic removeObjectForKey:groupName]; [self.sortKeysArray removeObject:groupName]; } [tableView reloadData];}// 添加数据- (void)addNewModel:(ContactModel *)newModel tableView:(UITableView *)tableView{ NSString *newGroupName = [[newModel.name pinyinOfName] firstCharacterOfName]; NSMutableArray *newValue = [self.dataDic objectForKey:newGroupName]; if (newValue == nil) { newValue = [NSMutableArray array]; [newValue addObject:newModel]; [self.dataDic setObject:newValue forKey:newGroupName]; [self.sortKeysArray addObject:newGroupName]; self.sortKeysArray = [NSMutableArray arrayWithArray:[self.sortKeysArray sortedArrayUsingSelector:@selector(compare:)]]; }else{ [newValue addObject:newModel]; } [tableView reloadData]; }// 实现返回分区数- (NSInteger)numberOfSection{ return self.dataDic.count;}// 返回每个分区有多少行- (NSInteger)numberOfRowsInSection:(NSInteger)section{ // 用分区取key NSString *key = self.sortKeysArray[section]; // 用key取values NSArray *values = self.dataDic[key]; // 返回value的个数 return values.count;}// 根据索引返回字典中的model- (ContactModel *)cellModelAtIndexPath:(NSIndexPath *)indexPath{ NSString *key = self.sortKeysArray[indexPath.section]; NSArray *values = self.dataDic[key]; ContactModel *model = values[indexPath.row]; return model;}// 返回分区表头- (NSString *)titleForCellHeaderInSection:(NSInteger)section{ return self.sortKeysArray[section];}// 返回侧边按钮- (NSArray *)rightButtonForCell{ return self.sortKeysArray;}// 提交编辑- (void)tableView:(UITableView *)tableView forRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *key = self.sortKeysArray[indexPath.section]; NSMutableArray *values = self.dataDic[key]; // 删除数据 [values removeObjectAtIndex:indexPath.row]; // 刷新UI页面 if (values.count == 0) { // 把数组的key也删了 // values 中没有元素了 需要删除分区 [self.dataDic removeObjectForKey:key]; [self.sortKeysArray removeObject:key]; // 刷新分区的方法 NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section]; [tableView deleteSections:indexSet withRowAnimation:(UITableViewRowAnimationLeft)]; }else{ // 直接刷新 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)]; } }// 提交移动- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ NSString *key = self.sortKeysArray[sourceIndexPath.section]; NSMutableArray *values = self.dataDic[key]; ContactModel *model = [values[sourceIndexPath.row] retain]; // 要先retain一下 不然删除的时候 会引用计数减一 被释放 // 从来源的索引处删除元素 [values removeObjectAtIndex:sourceIndexPath.row]; // 按目的地索引插入 [values insertObject:model atIndex:destinationIndexPath.row]; // 插入数组后 释放 [model release]; // 刷新UI界面 [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];}@end
创建一个ContactModel类来接受字典中的数据
.h
//// ContactModel.h// AddressBook//// Created by lanou on 15/11/25.// Copyright (c) 2015年 ZhangFengtian. All rights reserved.//#import <Foundation/Foundation.h>@interface ContactModel : NSObject@property(nonatomic, retain)NSString *name; // 姓名@property(nonatomic, retain)NSString *age; // 年龄@property(nonatomic, retain)NSString *gender; // 性别@property(nonatomic, retain)NSString *phoneNumber; // 号码@property(nonatomic, retain)NSString *hobby; // 爱好@property(nonatomic, retain)NSString *picture; // 照片@end
.m
//// ContactModel.m// AddressBook//// Created by lanou on 15/11/25.// Copyright (c) 2015年 ZhangFengtian. All rights reserved.//#import "ContactModel.h"@implementation ContactModel-(void)dealloc{ [_age release]; [_name release]; [_gender release]; [_hobby release]; [_phoneNumber release]; [_picture release]; [super dealloc];}- (void)setValue:(id)value forUndefinedKey:(NSString *)key{ }@end
详情和添加页面的布局(自觉忽略美观,重在传值)
.h
//// StudentView.h// MyAddressBook//// Created by long on 15/8/9.// Copyright (c) 2015年 WLong. All rights reserved.//#import <UIKit/UIKit.h>@interface StudentView : UIView@property (nonatomic,retain) UILabel *nameLabel;@property (nonatomic,retain) UITextField *nameTF;@property (nonatomic,retain) UILabel *sexLb;@property (nonatomic,retain) UITextField *sexTF;@property (nonatomic,retain) UILabel *phoneNumberLb;@property (nonatomic,retain) UITextField *phoneNumberTF;@property (nonatomic,retain) UILabel *pictureLb;@property (nonatomic,retain) UITextField *pictureTF;@property (nonatomic,retain) UILabel *hobbyLb;@property (nonatomic,retain) UITextField *hobbyTF;@property (nonatomic,retain) UILabel *ageLb;@property (nonatomic,retain) UITextField *ageTF;@end
.m
//// StudentView.m// MyAddressBook//// Created by long on 15/8/9.// Copyright (c) 2015年 WLong. All rights reserved.//#import "StudentView.h"@implementation StudentView- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor orangeColor]; [self addView]; } return self;}- (void)addView{ _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 80, 60, 30)]; _nameLabel.text = @"姓名:"; [self addSubview:_nameLabel]; _nameTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 80, 180, 30)]; _nameTF.borderStyle = UITextBorderStyleLine; _nameTF.placeholder = @"your name"; [self addSubview:_nameTF]; _sexLb = [[UILabel alloc] initWithFrame:CGRectMake(40, 140, 60, 30)]; _sexLb.text = @"性别:"; [self addSubview:_sexLb]; _sexTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 140, 180, 30)]; _sexTF.borderStyle = UITextBorderStyleLine; _sexTF.placeholder = @"your new Sex"; [self addSubview:_sexTF]; _phoneNumberLb = [[UILabel alloc] initWithFrame:CGRectMake(40, 180, 60, 30)]; _phoneNumberLb.text = @"电话:"; [self addSubview:_phoneNumberLb]; _phoneNumberTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 180, 180, 30)]; _phoneNumberTF.borderStyle = UITextBorderStyleLine; _phoneNumberTF.placeholder = @"your new phoneNumber"; [self addSubview:_phoneNumberTF]; _pictureLb = [[UILabel alloc] initWithFrame:CGRectMake(40, 220, 60, 30)]; _pictureLb.text = @"图片:"; [self addSubview:_pictureLb]; _pictureTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 220, 180, 30)]; _pictureTF.borderStyle = UITextBorderStyleLine; _pictureTF.placeholder = @"your new picture"; [self addSubview:_pictureTF]; _hobbyLb = [[UILabel alloc] initWithFrame:CGRectMake(40, 260, 60, 30)]; _hobbyLb.text = @"爱好:"; [self addSubview:_hobbyLb]; _hobbyTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 260, 180, 30)]; _hobbyTF.borderStyle = UITextBorderStyleLine; _hobbyTF.placeholder = @"your new hobby"; [self addSubview:_hobbyTF]; _ageLb = [[UILabel alloc] initWithFrame:CGRectMake(40, 300, 60, 30)]; _ageLb.text = @"年龄:"; [self addSubview:_ageLb]; _ageTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 180, 30)]; _ageTF.borderStyle = UITextBorderStyleLine; _ageTF.placeholder = @"your new age"; [self addSubview:_ageTF];}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code}*/@end
最后留下一个类目 把汉字转为拼音 然后截取拼音的首字母
NSString+Characters.h
//// NSString+Characters.h// AddressBook//// Created by y_小易 on 14-6-10.// Copyright (c) 2014年 www.lanou3g.com. All rights reserved.//#import <Foundation/Foundation.h>@interface NSString (Characters)//讲汉字转换为拼音- (NSString *)pinyinOfName;//汉字转换为拼音后,返回大写的首字母- (NSString *)firstCharacterOfName;@end
NSString+Characters.m
//// NSString+Characters.m// AddressBook//// Created by y_小易 on 14-6-10.// Copyright (c) 2014年 www.lanou3g.com. All rights reserved.//#import "NSString+Characters.h"@implementation NSString (Characters)//讲汉字转换为拼音- (NSString *)pinyinOfName{ NSMutableString * name = [[[NSMutableString alloc] initWithString:self ] autorelease]; CFRange range = CFRangeMake(0, 1); // 汉字转换为拼音,并去除音调 if ( ! CFStringTransform((__bridge CFMutableStringRef) name, &range, kCFStringTransformMandarinLatin, NO) || ! CFStringTransform((__bridge CFMutableStringRef) name, &range, kCFStringTransformStripDiacritics, NO)) { return @""; } return name;}//汉字转换为拼音后,返回大写的首字母- (NSString *)firstCharacterOfName{ NSMutableString * first = [[[NSMutableString alloc] initWithString:[self substringWithRange:NSMakeRange(0, 1)]] autorelease]; CFRange range = CFRangeMake(0, 1); // 汉字转换为拼音,并去除音调 if ( ! CFStringTransform((__bridge CFMutableStringRef) first, &range, kCFStringTransformMandarinLatin, NO) || ! CFStringTransform((__bridge CFMutableStringRef) first, &range, kCFStringTransformStripDiacritics, NO)) { return @""; } NSString * result; result = [first substringWithRange:NSMakeRange(0, 1)]; return result.uppercaseString;}@end
0 0
- 实现通讯录功能-使用单例封装写法
- 通讯录封装实现
- 实现通讯录的检索功能(本例中使用了观察者模式,需要多多用心)
- Swift多种实现单例写法
- PHP分页功能使用类封装实现
- 私人通讯录功能详细实现
- 实现简单功能的通讯录
- PinnedSectionListView实现手机通讯录功能
- 单例的封装创建与使用
- 使用单例模式封装Intent(实现跳转与携带数据跳转,顺带发送广播)
- 单例写法
- iOS 单例写法
- 单例的写法
- 单例的写法
- 非标准单例写法
- OC单例写法
- 单例各种写法
- ios 单例写法
- AsOne Auto Body Parts and Truck Parts
- 《启示录》阅读笔记(8)——产品开发伙伴+市场调研
- 调试时C++报告`scalar deleting destructor`问题的解决
- 在创业公司的那些日子(一)
- c++文件操作详解
- 实现通讯录功能-使用单例封装写法
- 让程序在远程主机后台运行 (&、nohuo、 screen)Linux 使用技巧
- java转web项目中遇到的问题及解决
- [JavaScript/JQuery] jquery 复选框全选 全不选 反选
- 查看自己mysql的字符集
- 端口映射
- content provider示意图
- 使用大图+脚本,生成各种size的app icon和图片素材
- linux上安装libpng库以及zlib库