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實現聯系人列表

示圖:

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: