1.使用UITableView,实现联系人字母排序、点击字母跳转显示联系人组目录;
2.使用UISearchController,实现联系搜索,动态显示符合查询的联系人;
3.点击通讯录列表项,显示联系人信息(使用自定义模式化窗口类似与UIAlertView,使用UIwindow实现),点击拨号,可以直接拨打电话;
4.实现获取手机通讯录里面的联系人信息;
详情见资源:http://download.youkuaiyun.com/detail/u011622479/9505751
效果图如下:
获取联系人:
搜索页:
联系人信息:
主要显示页面代码:
//
// ViewController.m
// ContactionView
//
// Created by rong xiang on 16/4/26.
// Copyright © 2016年 hzz. All rights reserved.
//
#import "ViewController.h"
#import <AddressBook/AddressBook.h>
#import "MobileAddressBook.h"
#import "ChineseString.h"
#import "CustomAlertView.h"
#import "MyButton.h"
@interface ViewController (){
NSMutableArray * listSection;
NSMutableArray * addressBookTemp;
NSMutableArray * listPhone;
NSMutableArray *_searchContacts;//符合条件的搜索联系人
CustomAlertView * alertView;
UITableView *tableViewAddress;
UISearchBar * _searchBar;
UISearchDisplayController *_searchDisplayController;
}
@end
@implementation ViewController
-(void) loadView{
[super loadView];
tableViewAddress = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)];
[tableViewAddress setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:250.0f/255.0f blue:250.0f/255.0f alpha:1.0]];
[tableViewAddress setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.view addSubview:tableViewAddress];
[self addSearchBar];
}
- (void)viewDidLoad {
[super viewDidLoad];
//初始化显示对象
addressBookTemp = [[NSMutableArray alloc] init];
listSection = [[NSMutableArray alloc] init];
listPhone =[[NSMutableArray alloc] init];
NSMutableArray * listPhoneShow = [[NSMutableArray alloc] init];
tableViewAddress.delegate = self;
tableViewAddress.dataSource = self;
//获取通讯录联系人信息
[self getAddressBook];
//测试下的:初始化列表数据
[self initData];
//获取通讯录列表首字母,并排序
listSection = [ChineseString IndexArray:addressBookTemp];
//获取通讯录,并把通讯录对象按照首字母进行分组排序
listPhoneShow = [ChineseString LetterSortArray:addressBookTemp];
//把对应的同一个首字母下联系人对象按照首字母排序列表进行分组;
NSInteger count = [listPhoneShow count];
NSArray * array = nil;
for(int i =0;i<count;i++){
array =[listPhoneShow objectAtIndex:i];
NSInteger arrCount = [array count];
NSMutableArray * showArr = [[NSMutableArray alloc] init];
for(int j =0;j< arrCount;j++){
NSString *tempStr = [array objectAtIndex:j];
for(MobileAddressBook * add in addressBookTemp){
if([[add name] isEqualToString:tempStr]){
add.firstName = [listSection objectAtIndex:i];
[showArr addObject:add];
break;
}
}
}
[listPhone addObject:showArr];
}
}
-(void) dealloc{
tableViewAddress.dataSource = nil;
tableViewAddress.delegate = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark 搜索形成新数据
-(void)searchDataWithKeyWord:(NSString *)keyWord{
//_isSearching=YES;
_searchContacts=[NSMutableArray array];
int count = [listPhone count];
int arrCount = 0;
NSArray * arr = nil;
MobileAddressBook * contact = nil;
//过滤
for(int i=0;i<count;i++){
arr = [listPhone objectAtIndex:i];
arrCount = arr.count;
for(int j=0;j<arrCount;j++){
contact = [arr objectAtIndex:j];
if ([contact.firstName.uppercaseString containsString:keyWord.uppercaseString]||[contact.name.uppercaseString containsString:keyWord.uppercaseString]||[contact.tel containsString:keyWord]) {
[_searchContacts addObject:contact];
}
}
}
// [listPhone enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// NSArray * arr =obj;
// [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// MobileAddressBook * contact = obj;
// if ([contact.firstName.uppercaseString containsString:keyWord.uppercaseString]|