OC -类似于通讯录的功能
2017-02-16 10:17
162 查看
一.建立person类包含name pinyin参数
#import "Person.h"
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *pinYin;
二.实现类
#import "Person.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *dataSource;
UITableView *myTableView;
NSMutableArray *titleAray;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"UITableView";
self.view.backgroundColor = [UIColor lightGrayColor];
dataSource = [NSMutableArray array];
NSArray *arr = [[NSArray alloc] initWithObjects:@"王达",@"李明",@"李松",@"朱子夫",@"秦始皇",@"刘彻",@"李世民",@"周树人",@"李松达",@"邓紫棋",@"安美",@"安菲",@"崔杰",@"冰冰",@"啊Q", nil];
NSMutableArray *modelArray = [NSMutableArray array];
for (int i = 0; i< arr.count; i++) {
NSMutableString *mutableString = [NSMutableString stringWithString:arr[i]];
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripDiacritics, false);
Person *model = [[Person alloc] init];
model.name = arr[i];
model.pinYin = mutableString;
[modelArray addObject:model];
}
//NSSortDescriptor 指定用于对象数组排序的对象的属性
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]];
//对person进行排序
[modelArray sortUsingDescriptors:sortDescriptors];
titleAray = [NSMutableArray array];
//删除多余的字符
for (int i = 0; i < modelArray.count; i++) {
Person *model = modelArray[i];
NSString *str = [model.pinYin substringToIndex:1];
if (![titleAray containsObject:str]) {
[titleAray addObject:str];
}
}
//将中文数据进行二维的数组的拆分
for (int i = 0; i< titleAray.count; i++) {
NSString *str = titleAray[i];
NSMutableArray *sortArray = [NSMutableArray array];
for (Person *model in modelArray) {
if ([model.pinYin hasPrefix:str]) {
[sortArray addObject:model.name];
}
}
[dataSource addObject:sortArray];
}
[self creatTableView];
}
#pragma mark - UITableViewDelegate
-(void)creatTableView{
myTableView = [[UITableView alloc] initWithFrame:self.view.frame];
myTableView.delegate = self;
myTableView.dataSource = self;
//设置右侧索引的背景色
myTableView.sectionIndexBackgroundColor = [UIColor clearColor];
[self.view addSubview:myTableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dataSource[section] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 55;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray *toBeReturned = [[NSMutableArray alloc]init];
for(char c = 'A';c<='Z';c++)
[toBeReturned addObject:[NSString stringWithFormat:@"%c",c]];
return toBeReturned;
}
#pragma mark - UITableViewDataSource
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
view.backgroundColor = [UIColor grayColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 60, 20)];
NSString *str = titleAray[section];
//转化为大写
str = [str uppercaseString];
label.text = str;
[view addSubview:label];
return view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
cell.textLabel.text = dataSource[indexPath.section][indexPath.row];
return cell;
}
#import "Person.h"
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *pinYin;
二.实现类
#import "Person.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *dataSource;
UITableView *myTableView;
NSMutableArray *titleAray;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"UITableView";
self.view.backgroundColor = [UIColor lightGrayColor];
dataSource = [NSMutableArray array];
NSArray *arr = [[NSArray alloc] initWithObjects:@"王达",@"李明",@"李松",@"朱子夫",@"秦始皇",@"刘彻",@"李世民",@"周树人",@"李松达",@"邓紫棋",@"安美",@"安菲",@"崔杰",@"冰冰",@"啊Q", nil];
NSMutableArray *modelArray = [NSMutableArray array];
for (int i = 0; i< arr.count; i++) {
NSMutableString *mutableString = [NSMutableString stringWithString:arr[i]];
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripDiacritics, false);
Person *model = [[Person alloc] init];
model.name = arr[i];
model.pinYin = mutableString;
[modelArray addObject:model];
}
//NSSortDescriptor 指定用于对象数组排序的对象的属性
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]];
//对person进行排序
[modelArray sortUsingDescriptors:sortDescriptors];
titleAray = [NSMutableArray array];
//删除多余的字符
for (int i = 0; i < modelArray.count; i++) {
Person *model = modelArray[i];
NSString *str = [model.pinYin substringToIndex:1];
if (![titleAray containsObject:str]) {
[titleAray addObject:str];
}
}
//将中文数据进行二维的数组的拆分
for (int i = 0; i< titleAray.count; i++) {
NSString *str = titleAray[i];
NSMutableArray *sortArray = [NSMutableArray array];
for (Person *model in modelArray) {
if ([model.pinYin hasPrefix:str]) {
[sortArray addObject:model.name];
}
}
[dataSource addObject:sortArray];
}
[self creatTableView];
}
#pragma mark - UITableViewDelegate
-(void)creatTableView{
myTableView = [[UITableView alloc] initWithFrame:self.view.frame];
myTableView.delegate = self;
myTableView.dataSource = self;
//设置右侧索引的背景色
myTableView.sectionIndexBackgroundColor = [UIColor clearColor];
[self.view addSubview:myTableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dataSource[section] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 55;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray *toBeReturned = [[NSMutableArray alloc]init];
for(char c = 'A';c<='Z';c++)
[toBeReturned addObject:[NSString stringWithFormat:@"%c",c]];
return toBeReturned;
}
#pragma mark - UITableViewDataSource
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
view.backgroundColor = [UIColor grayColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 60, 20)];
NSString *str = titleAray[section];
//转化为大写
str = [str uppercaseString];
label.text = str;
[view addSubview:label];
return view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
cell.textLabel.text = dataSource[indexPath.section][indexPath.row];
return cell;
}
相关文章推荐
- 最近实现一个类似于微信通讯录的功能//
- 用OC语言完成简易通讯录(实现增删改查)功能.
- 类似于通讯录的搜索功能
- OC: 用OC语言完成简易通讯录(实现增删改查)功能.
- 用OC语言完成简易通讯录(实现增删改查)功能.
- 类似于开心网消息提醒功能
- 实现类似于百度实时搜索将结果在下拉框中显示的功能
- JQuery ui 实现类似于confirm的功能
- IOS通讯录的隐藏标签(电话)的特殊功能
- C# 画图中类似于建模的参考坐标系功能
- 用文件实现类似于数据库一样的功能(老古董,怀念用)
- 实现类似于sharepoint列表的分组统计功能
- OC-字典&数组运用实例:通讯录的实现
- SQL实现类似于自动刷新数据的功能
- 通讯录的常用功能保存
- SVN Externals Definitions[类似于软链接的功能]
- iOS开发之--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook功能开发汇总
- 用OC语⾔言完成简易通讯录(实现增删改查)功 能. (注:使用MRC)
- Oc 录音机 功能(系统方法)~dome
- web通讯录之搜索功能