概述
本文实例为大家分享了iOS实现联系人列表功能的具体代码,供大家参考,具体内容如下
按照顺序排列联系人列表,需要引入一些工具(详见demo):
主要部分代码:
#import "LinkMan.h" #import "LinkmanCell.h" #import "LinkmanSelectCell.h" #import "pinyin.h" #import "ChineseString.h" @interface LinkMan ()<UISearchBarDelegate,UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource> @property (weak, nonatomic) IBOutlet UILabel *titleLab; //标题 @property (weak, nonatomic) IBOutlet UIView *titleView; //标题栏 @property (weak, nonatomic) IBOutlet UITableView*gTableView; //联系人TV @property (weak, nonatomic) IBOutlet UIButton *sureBut; //确定 @property (nonatomic ,strong) NSMutableArray *gPeopleArr; //一级联系人 @property (nonatomic ,strong) NSMutableArray *gMarkKeyArr; //标识 @property (nonatomic ,strong) NSMutableArray *gLinkManArr; //二级联系人 @end @implementation LinkMan @synthesize gTableView; @synthesize gPeopleArr; @synthesize gLinkManArr; @synthesize gMarkKeyArr; -(void)dealloc{ gPeopleArr = nil; gMarkKeyArr = nil; } - (void)viewDidLoad { [super viewDidLoad]; //清空选择联系人 NSUserDefaults *userInfoDefaults = [NSUserDefaults standardUserDefaults]; [userInfoDefaults setObject:nil forKey:@"linkmanSelectList"]; [userInfoDefaults synchronize]; //标题栏 [self cTitleBar]; //右侧选择栏 gMarkKeyArr = [[NSMutableArray alloc] init]; gPeopleArr = [[NSMutableArray alloc] init]; gLinkManArr = [[NSMutableArray alloc] init]; //网络数据请求 [self cNetWorkLinkman]; } //标题栏 -(void)cTitleBar{ int ver = [[[UIDevice currentDevice] systemVersion] intValue]; float height = 44; if(ver >= 7){ height = 64; } CGRect titleRect = _titleView.frame; titleRect.size.height = height; [_titleView setFrame:titleRect]; } //返回 - (IBAction)returnAction:(id)sender { //联系人可多选 NSUserDefaults *userInfoDefaults = [NSUserDefaults standardUserDefaults]; [userInfoDefaults setObject:@"0" forKey:@"linkmanSelect"]; [userInfoDefaults synchronize]; //二级 if(gLinkManArr.count > 0){ [gLinkManArr removeAllObjects]; [_titleLab setText:@"联系人"]; [gTableView reloadData]; }else{//一级 NSUserDefaults *noneDefaults = [NSUserDefaults standardUserDefaults]; [noneDefaults setObject:@"1" forKey:@"noneproject"]; [noneDefaults synchronize]; [self.navigationController popViewControllerAnimated:YES ]; } } //按照拼音首字母排序 - (NSMutableArray *)getChineseStringArr:(NSMutableArray *)arrToSort { NSMutableArray *chineseStringsArray = [NSMutableArray array]; for(int i = 0; i < [arrToSort count]; i++) { ChineseString *chineseString=[[ChineseString alloc]init]; NSDictionary *tempDict = (NSDictionary *)[arrToSort objectAtIndex:i]; chineseString.string=[NSString stringWithString:[tempDict objectForKey:@"name"]]; chineseString.pID = [NSString stringWithString:[tempDict objectForKey:@"id"]]; if(chineseString.string==nil){ chineseString.string=@""; } if(![chineseString.string isEqualToString:@""]){ //join the pinYin NSString *pinYinResult = [NSString string]; for(int j = 0;j < chineseString.string.length; j++) { NSString *singlePinyinLetter = [[NSString stringWithFormat:@"%c", pinyinFirstLetter([chineseString.string characterAtIndex:j])]uppercaseString]; pinYinResult = [pinYinResult stringByAppendingString:singlePinyinLetter]; } chineseString.pinYin = pinYinResult; } else { chineseString.pinYin = @""; } [chineseStringsArray addObject:chineseString]; // [chineseString release]; } //sort the ChineseStringArr by pinYin 拼音排序 NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]]; [chineseStringsArray sortUsingDescriptors:sortDescriptors]; NSMutableArray *arrayForArrays = [NSMutableArray array]; BOOL checkValueAtIndex= NO; //flag to check NSMutableArray *TempArrForGrouping = nil; [gMarkKeyArr removeAllObjects]; for(int index = 0; index < [chineseStringsArray count]; index++) { ChineseString *chineseStr = (ChineseString *)[chineseStringsArray objectAtIndex:index]; NSMutableString *strchar= [NSMutableString stringWithString:chineseStr.pinYin]; NSString *sr= [strchar substringToIndex:1]; if(![gMarkKeyArr containsObject:[sr uppercaseString]])//在这里我检查字符是否已经选择头键 { [gMarkKeyArr addObject:[sr uppercaseString]]; TempArrForGrouping = [[NSMutableArray alloc] initWithObjects:nil]; checkValueAtIndex = NO; } if([gMarkKeyArr containsObject:[sr uppercaseString]]) { [TempArrForGrouping addObject:[chineseStringsArray objectAtIndex:index]]; if(checkValueAtIndex == NO) { [arrayForArrays addObject:TempArrForGrouping]; checkValueAtIndex = YES; } } } return arrayForArrays; } //数据准备 -(NSMutableDictionary *)linkmanAction:(NSMutableDictionary *)listDict{ NSMutableArray *branchPersonArr = [[NSMutableArray alloc] init]; for (int k = 1; k < 20; k ++) { NSMutableDictionary *branchPersonDict = [[NSMutableDictionary alloc] init]; NSInteger arcRandom = arc4random()%1000; [branchPersonDict setObject:[NSString stringWithFormat:@"%d",arcRandom*k] forKey:@"id"]; [branchPersonDict setObject:[NSString stringWithFormat:@"%d",arcRandom*k] forKey:@"name"]; NSMutableArray *branchArr = [[NSMutableArray alloc] init]; for (int i = 1; i < 20; i ++) { NSMutableDictionary *branchDict = [[NSMutableDictionary alloc] init]; NSInteger arcRandom = arc4random()%10000 + 10000; [branchDict setObject:[NSString stringWithFormat:@"%d",arcRandom*i] forKey:@"id"]; [branchDict setObject:[NSString stringWithFormat:@"%d",arcRandom*i] forKey:@"name"]; [branchDict setObject:@"110" forKey:@"phone"]; [branchDict setObject:@"" forKey:@"header"]; [branchArr addObject:branchDict]; } [branchPersonDict setObject:branchArr forKey:@"member"]; [branchPersonArr addObject:branchPersonDict]; } [listDict setObject:branchPersonArr forKey:@"branch"]; return listDict; } //网络数据请求 -(void)cNetWorkLinkman{ NSMutableDictionary *listDict = [[NSMutableDictionary alloc] init]; listDict = [self linkmanAction:listDict]; NSArray *branchArr = [listDict objectForKey:@"branch"]; NSArray *projectArr; NSMutableArray *tempArr = [branchArr mutableCopy]; for (int j = 0; j <projectArr.count; j ++) { NSDictionary *prodict = [projectArr objectAtIndex:j]; [tempArr addObject:prodict]; } //从网络接口获取数据 if(listDict){ //联系人列表 NSUserDefaults *userInfoDefaults = [NSUserDefaults standardUserDefaults]; [userInfoDefaults setObject:[self chineseStringArr:tempArr] forKey:@"linkmanList"]; [userInfoDefaults synchronize]; [gTableView reloadData]; } } //几组 - (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView { if(gLinkManArr.count > 0){//二级 if(gLinkManArr!=nil && gLinkManArr.count > 0){ gLinkManArr = [self getChineseStringArr:gLinkManArr]; return gLinkManArr.count; } }else{//一级 [_titleLab setText:@"联系人"]; //联系人列表 NSUserDefaults *userInfoDefaults = [NSUserDefaults standardUserDefaults]; NSMutableArray *linkmanMarr = [userInfoDefaults objectForKey:@"linkmanList"]; if(linkmanMarr!=nil && linkmanMarr.count > 0){ gPeopleArr = [self getChineseStringArr:linkmanMarr]; return gPeopleArr.count; } } return 0; } //几行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { [_sureBut setHidden:YES]; if(gLinkManArr.count > 0){//二级 [_sureBut setHidden:NO]; NSArray *temp = [gLinkManArr objectAtIndex:section]; return temp.count; }else{//一级 //联系人列表 NSUserDefaults *userInfoDefaults = [NSUserDefaults standardUserDefaults]; NSMutableArray *linkmanMarr = [userInfoDefaults objectForKey:@"linkmanList"]; if(linkmanMarr!=nil && linkmanMarr.count > 0){ //联系人状态 gPeopleArr = [self getChineseStringArr:linkmanMarr]; NSArray *temp = [gPeopleArr objectAtIndex:section]; return temp.count; } } return 0; } //*字母排序搜索 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return gMarkKeyArr; } //搜索时显示按索引第几组 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ NSInteger count = 0; for(NSString *character in gMarkKeyArr){ if([character isEqualToString:title]){ return count; } count ++; } return count; } //标头 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [gMarkKeyArr objectAtIndex:section]; } //TableView赋值 - (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath { //联系人可多选 NSUserDefaults *userInfoDefaults = [NSUserDefaults standardUserDefaults]; NSString *linkmanSelect = [userInfoDefaults objectForKey:@"linkmanSelect"]; if([linkmanSelect isEqualToString:@"1"]){ static NSString *cellIdentifier=@"LinkmanSelectCell"; LinkmanSelectCell *cell=[tableview dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = (LinkmanSelectCell*)[[[NSBundle mainBundle] loadNibNamed:@"LinkmanSelectCell" owner:nil options:nil] objectAtIndex:0]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } cell.backgroundColor=[UIColor clearColor]; [cell setClipsToBounds:YES]; //二级 if(gLinkManArr.count > 0){ if(gLinkManArr.count > indexPath.section){ NSArray *onegp = [gLinkManArr objectAtIndex:indexPath.section]; ChineseString *strLinkMan = [onegp objectAtIndex:indexPath.row]; [cell cSetValueID:strLinkMan.pID andLinkMan:strLinkMan.string andBranch:@"" andPhoneNum:nil]; }else{ [cell cSetValueID:@"" andLinkMan:@"" andBranch:@"" andPhoneNum:nil]; } }else{//一级 if(gPeopleArr.count > indexPath.section){ NSArray *onegp = [gPeopleArr objectAtIndex:indexPath.section]; ChineseString *strLinkMan = [onegp objectAtIndex:indexPath.row]; [cell cSetValueID:strLinkMan.pID andLinkMan:strLinkMan.string andBranch:@"" andPhoneNum:nil]; }else{ [cell cSetValueID:@"" andLinkMan:@"" andBranch:@"" andPhoneNum:nil]; } } return cell; }else{ static NSString *cellIdentifier=@"LinkmanCell"; LinkmanCell *cell=[tableview dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = (LinkmanCell*)[[[NSBundle mainBundle] loadNibNamed:@"LinkmanCell" owner:nil options:nil] objectAtIndex:0]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } cell.backgroundColor=[UIColor clearColor]; [cell setClipsToBounds:YES]; //二级 if(gLinkManArr.count > 0){ if(gLinkManArr.count > indexPath.section){ NSArray *onegp = [gLinkManArr objectAtIndex:indexPath.section]; ChineseString *strLinkMan = [onegp objectAtIndex:indexPath.row]; [cell cSetValueID:strLinkMan.pID andLinkMan:strLinkMan.string andBranch:@"" andPhoneNum:nil]; }else{ [cell cSetValueID:@"" andLinkMan:@"" andBranch:@"" andPhoneNum:nil]; } }else{//一级 if(gPeopleArr.count > indexPath.section){ NSArray *onegp = [gPeopleArr objectAtIndex:indexPath.section]; ChineseString *strLinkMan = [onegp objectAtIndex:indexPath.row]; [cell cSetValueID:strLinkMan.pID andLinkMan:strLinkMan.string andBranch:@"" andPhoneNum:nil]; }else{ [cell cSetValueID:@"" andLinkMan:@"" andBranch:@"" andPhoneNum:nil]; } } return cell; } } //选中时执行的操作 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self.view endEditing:YES]; LinkmanCell *tempCell = (LinkmanCell *)[tableView cellForRowAtIndexPath:indexPath]; //二级 if(gLinkManArr.count > 0){ }else{//一级 NSInteger theIndex = 0; for (int i = 0; i <= indexPath.section ; i ++) { NSArray *tempPeoArr = [gPeopleArr objectAtIndex:i]; if(i == indexPath.section){ theIndex = theIndex + indexPath.row; }else{ theIndex = theIndex + tempPeoArr.count; } } //联系人列表 NSUserDefaults *userInfoDefaults = [NSUserDefaults standardUserDefaults]; NSMutableArray *linkmanMarr = [userInfoDefaults objectForKey:@"linkmanList"]; NSDictionary *linkmanDict; if(linkmanMarr.count > theIndex){ linkmanDict = [linkmanMarr objectAtIndex:theIndex]; }else{ return; } NSArray *linkArr = [linkmanDict objectForKey:@"member"]; if(linkArr.count > 0){ gLinkManArr = [linkArr mutableCopy]; [_titleLab setText:tempCell.gLinkMan.text]; //联系人可多选 NSUserDefaults *userInfoDefaults = [NSUserDefaults standardUserDefaults]; [userInfoDefaults setObject:@"1" forKey:@"linkmanSelect"]; [userInfoDefaults synchronize]; [gTableView reloadData]; } } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 44.0; } //UITableViewDelegate,UITableViewDataSource - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(CGFLOAT_MIN, CGFLOAT_MIN, CGFLOAT_MIN, 22)]; [tempView setBackgroundColor:[UIColor colorWithRed:248.0/255 green:248.0/255 blue:248.0/255 alpha:1]]; UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 22)]; [titleLab setBackgroundColor:[UIColor clearColor]]; if(gMarkKeyArr.count > section){ [titleLab setText:[gMarkKeyArr objectAtIndex:section]]; }else{ [titleLab setText:@""]; } [titleLab setTextColor:[UIColor colorWithRed:34.0/255 green:34.0/255 blue:34.0/255 alpha:1]]; [titleLab setFont:[UIFont boldSystemFontOfSize:17]]; [tempView addSubview:titleLab]; return tempView; } //按照拼音首字母排序 - (NSMutableArray *)chineseStringArr:(NSMutableArray *)arrToSort { NSMutableArray *chineseStringsArray = [NSMutableArray array]; for(int i = 0; i < [arrToSort count]; i++) { ChineseString *chineseString=[[ChineseString alloc]init]; NSDictionary *tempDict = (NSDictionary *)[arrToSort objectAtIndex:i]; chineseString.string=[NSString stringWithString:[tempDict objectForKey:@"name"]]; chineseString.pID = [NSString stringWithString:[tempDict objectForKey:@"id"]]; //chineseString.bID = [NSString stringWithString:[tempDict objectForKey:@"bid"]]; if(chineseString.string==nil){ chineseString.string=@""; } if(![chineseString.string isEqualToString:@""]){ //join the pinYin NSString *pinYinResult = [NSString string]; for(int j = 0;j < chineseString.string.length; j++) { NSString *singlePinyinLetter = [[NSString stringWithFormat:@"%c", pinyinFirstLetter([chineseString.string characterAtIndex:j])]uppercaseString]; pinYinResult = [pinYinResult stringByAppendingString:singlePinyinLetter]; } chineseString.pinYin = pinYinResult; } else { chineseString.pinYin = @""; } [chineseStringsArray addObject:chineseString]; // [chineseString release]; } //sort the ChineseStringArr by pinYin 拼音排序 NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]]; [chineseStringsArray sortUsingDescriptors:sortDescriptors]; NSMutableArray *arrayForArrays = [[NSMutableArray alloc]init]; for (int i = 0; i < chineseStringsArray.count ; i ++) { ChineseString *tempChinese = [chineseStringsArray objectAtIndex:i]; NSString *chineseName = tempChinese.string; for (int j = 0; j < arrToSort.count ; j ++) { NSDictionary *tempSortDict = [arrToSort objectAtIndex:j]; NSString *sortName = [tempSortDict objectForKey:@"name"]; if([chineseName isEqualToString:sortName]){ [arrayForArrays addObject:tempSortDict]; } } } return arrayForArrays; } //确定 - (IBAction)sureAction:(id)sender { //已选择联系人 NSUserDefaults *userInfoDefaults = [NSUserDefaults standardUserDefaults]; NSMutableArray *selectList = [userInfoDefaults objectForKey:@"linkmanSelectList"]; if(selectList != nil){ NSString *messageStr = [NSString stringWithFormat:@"您选择了%d个对象",selectList.count]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:messageStr delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; [userInfoDefaults setObject:nil forKey:@"linkmanSelectList"]; [userInfoDefaults synchronize]; } } @end
Demo:iOS实现联系人列表
示图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是温暖鸡为你收集整理的iOS实现联系人列表功能的全部内容,希望文章能够帮你解决iOS实现联系人列表功能所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复