iOS实现联系人列表功能

 更新时间:2021年08月25日 15:34:00   作者:风浅月明  
这篇文章主要为大家详细介绍了iOS实现联系人列表功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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 改变键盘颜色代码

    这篇文章主要介绍了IOS 改变键盘颜色代码,十分的简单实用,有需要的小伙伴可以参考下。
    2015-05-05
  • IOS 开发之 UITextField限制字数的方法

    IOS 开发之 UITextField限制字数的方法

    这篇文章主要介绍了IOS 开发之 UITextField限制字数的方法的相关资料,这里提供实现限制最大字数的方法,需要的朋友可以参考下
    2017-08-08
  • IOS微信端confirm以及alert去掉网址的实例代码

    IOS微信端confirm以及alert去掉网址的实例代码

    下面小编就为大家分享一篇IOS微信端confirm以及alert去掉网址的实例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • iOS模仿电子书首页实现书架布局样式

    iOS模仿电子书首页实现书架布局样式

    这篇文章主要为大家详细介绍了iOS实现类似电子书首页效果样式,实现书架布局样式,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • ios开发Flutter之数据存储

    ios开发Flutter之数据存储

    这篇文章主要为大家介绍了ios开发Flutter之数据存储的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • iOS滑动解锁、滑动获取验证码效果的实现代码

    iOS滑动解锁、滑动获取验证码效果的实现代码

    这篇文章主要介绍了iOS滑动解锁、滑动获取验证码效果的实现代码,小编认为还不错,非常实用,所有分享给大家,感兴趣的朋友可以参考下
    2016-05-05
  • IOS ObjectiveC中的赋值与对象拷贝

    IOS ObjectiveC中的赋值与对象拷贝

    这篇文章主要介绍了IOS ObjectiveC中的赋值与对象拷贝的相关资料,希望通过本文能帮助到大家,让大家理解掌握这部分内容,需要的朋友可以参考下
    2017-09-09
  • 详细整理iOS中UITableView的性能优化

    详细整理iOS中UITableView的性能优化

    最近在微博上看到一个很好的开源项目,是关于如何优化UITableView的,加上正好最近也在优化项目中的类似朋友圈功能这块,思考了很多关于UITableView的优化技巧,所以决定详细的整理下对优化UITableView的理解,需要的朋友们可以参考借鉴。
    2017-03-03
  • iOS实现头部拉伸效果

    iOS实现头部拉伸效果

    这篇文章主要为大家详细介绍了iOS实现头部拉伸效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Navigation bar的注意事项详解

    Navigation bar的注意事项详解

    本文主要介绍了Navigation bar的注意事项。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01

最新评论