iOS实现联系人列表功能

时间:2021-07-20 14:49:39

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

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

iOS实现联系人列表功能

主要部分代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#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实现联系人列表功能

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

原文链接:https://gamin.blog.csdn.net/article/details/45095799

相关文章