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

示图:

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

(0)

相关推荐

  • IOS实现展开二级列表效果

    先来看看效果图 用法(类似UITableView) 初始化XDMultTableView #import "XDMultTableView.h" ... @property(nonatomic, readwrite, strong)XDMultTableView *tableView; _tableView = [[XDMultTableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, sel

  • iOS 标签Tag列表的实现代码

    前言 1.之前项目中会有一些标签列表来显示某些特性或要求,如下图(代码实现后的效果): 2.期间也是浏览了好多其他的第三方,但是可能是没找到好的方法去寻找吧,没有找到一个合适的,况且又不是特别复杂的东西,所以就自己写了一套,但是注意因为我们项目中使用的是RAC+Mansory,所以想要使用的话需要引入这两个库=_=. 3.自己写的时候考虑的不是太多,中心思想是ViewModel做定制需求,View通过ViewModel来实现定制化UI,其他更多的是逻辑上的排版,所以不做更多赘述,自己体会Y^o^

  • IOS展开三级列表效果示例

    效果图如下: #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end #import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate

  • iOS列表上拉(平滑加载数据)自动加载数据的问题解决

    项目需求 我的的列表需要改变,原来的分页加载采用的是MJRefresh框架进行加载更多数据,这需要有一个上拉动作才能触发,而我的产品的意思是当快要滑动到底部时自动加载下一页数据.我自己看了一下,发现很多app都是采用这种模式. 关于MJRefresh MJRefresh中并没有这样的方法,所以这个效果不一定是MJRefresh实现的,没有实现的小伙伴就不要在MJRefresh中苦苦寻找了. 功能实现 实现方法很简单,需要用到tableView的一个代理方法,就可轻松实现.- (void)tabl

  • iOS多级列表实现代码

    在项目开发中,层级列表经常遇到,简单点的二级列表利用UITableView的Header就可以实现,再简单点的三级列表通过对Cell高度进行调整也可以实现三级列表的效果.但遇到多级列表,尤其是层次不明的动态列表就比较麻烦了. 原理 层级列表和树形结构比较类似,不过不是二叉树,而是多叉树.每个节点只需要拥有指向父节点和子节点的两个指针,就能形成一颗树.我们将多级列表中每一级对象看作一个node,node拥有两个属性,分别为父节点和子节点的ID. 每棵树有个一个虚拟的root节点,它的ID为root

  • IOS实现简易版的QQ下拉列表

    下面我们通过实例代码来一步步看怎么实现, 首先建立了两个模型类, 一个Friend, 一个FriendGroup类. 数据源用的本地的一个plist文件. plist文件中包含了FriendGroup的name,friends数组等属性. Friend.h 示例代码 #import <Foundation/Foundation.h> @interface Friend : NSObject @property (nonatomic, copy) NSString *name; @end Fri

  • iOS实现列表折叠效果

    本文实例为大家分享了iOS实现列表折叠效果的具体代码,供大家参考,具体内容如下 实现列表折叠效果其实比较简单,点击列表头部的时候,把返回列表行数设为 0,就是收起列表:再次点击列表头部,显示列表的行数,就展开了列表. #import "TableDownUpVC.h" #import "TableViewCell_TableSelect.h" @interface TableDownUpVC () { NSMutableDictionary *dicSelet; N

  • ios基于UITableViewController实现列表

    实现效果图如下: News.h #import <Foundation/Foundation.h> @interface News : NSObject @property (nonatomic, strong) NSString *title; @property (nonatomic) NSUInteger count; @property (nonatomic, strong) NSString *imageName; + (NSArray *)demoData; @end<str

  • iOS开发之在列表上方添加水印的方法

    前言 为了防止工程师泄露用户信息,我们有个需求是在列表上面添加水印.我封装了这个视图分享出来.下面话不多说了,来一起看看详细的介绍吧 效果图 示例代码如下: watermarkView.h #import <UIKit/UIKit.h> @interface watermarkView : UIImageView /** 设置水印 @param frame 水印大小 @param markText 水印显示的文字 */ - (instancetype)initWithFrame:(CGRect

  • iOS实现从通讯录中选择联系人

    有时候APP需要用户输入一位联系人的姓名和电话,除了用户手动输入,一般也允许用户从通讯录中选择一位联系人(图1),下面的代码就是使用系统的<AddressBookUI/AddressBookUI.h>库实现这一需求. 图1 完整代码: #import "ViewController.h" #import <AddressBookUI/AddressBookUI.h> @interface ViewController ()<ABPeoplePickerNa

随机推荐