欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS实现联系人列表功能

程序员文章站 2022-06-23 12:26:56
本文实例为大家分享了ios实现联系人列表功能的具体代码,供大家参考,具体内容如下按照顺序排列联系人列表,需要引入一些工具(详见demo):主要部分代码:#import "linkman.h"#impo...

本文实例为大家分享了ios实现联系人列表功能的具体代码,供大家参考,具体内容如下

按照顺序排列联系人列表,需要引入一些工具(详见demo):

iOS实现联系人列表功能

主要部分代码:

#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实现联系人列表功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。