通訊錄作為通訊錄地址的書本。絕大多數(shù)的app中也都會添加通訊錄這樣的輔助功能,當(dāng)今的通訊錄可以涵蓋多項(xiàng)內(nèi)容,如:姓名、電話號碼、單位電話、移動電話、傳真號、電子郵件、QQ、MSN、個(gè)人主頁、公司、街道、郵編、生日、大頭帖、車牌、銀行帳號、俱樂部名稱、愛好等等。只要我們會寫一種,那其他根本不在話下!
首先,給大家來個(gè)通訊錄的樣圖:

在這里面要用到兩個(gè)第三方庫ZYPinYinSearchLib和LetterGroups它們確保通訊錄的排序方法。
下面讓大家看看通訊錄的主要代碼:
#import "ViewController.h"
#import "ZYPinYinSearch.h"
#import "ChineseString.h"
#import "FollwTableViewCell.h"
@interface ViewController ()<UISearchResultsUpdating>
@property (strong, nonatomic) UISearchController *searchController;
@property (nonatomic , strong)UITableView *tableView ;
@property (strong, nonatomic) NSArray *dataSource;/**<排序前的整個(gè)數(shù)據(jù)源*/
@property (strong, nonatomic) NSArray *allDataSource;/**<排序后的整個(gè)數(shù)據(jù)源*/
@property (strong, nonatomic) NSMutableArray *searchDataSource;/**< 搜索結(jié)果數(shù)據(jù)源*/
@property (strong, nonatomic) NSArray *indexDataSource;/**<索引數(shù)據(jù)源*/
@property (nonatomic ,strong) NSArray *ArrayIm;
@end
@implementation ViewController
- (void)viewWillDisappear:(BOOL)animated{
self.tableView.hidden = NO ;
[super viewWillDisappear:YES];
_searchController.active = NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.hidesBottomBarWhenPushed = YES;
self.navigationItem.title = @"關(guān)注";
[self.tableView setTableHeaderView:self.searchController.searchBar];
[self initData];
}
pragma mark - Init
- (void)initData {
_ArrayIm = @[@"01",@"02",@"03",@"04",@"05",@"02",@"03",@"04",@"05",@"02",@"03",@"04",@"05",@"02",@"03",@"04",@"05"];
_dataSource = @[@"成龍",@"梁山伯",@"Angel",@"長江1號",@"星爺",@"911",@"520ok",@"ren",@"++family",@"中english9%+",@"武松",@"齊天大圣",@"曹操",@"林黛玉",@"Bob",@"夏勒特",@"神雕俠"];
_searchDataSource = [NSMutableArray new];
//獲取索引的首字母
_indexDataSource = [ChineseString IndexArray:_dataSource];
//對原數(shù)據(jù)進(jìn)行排序重新分組
_allDataSource = [ChineseString LetterSortArray:_dataSource];
}
//搜索框的設(shè)置
- (UISearchController *)searchController {
if (!_searchController) {
_searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.hidesNavigationBarDuringPresentation = YES;
_searchController.searchBar.placeholder = @"搜索";
[_searchController.searchBar sizeToFit];
}
return _searchController;
}
pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (!self.searchController.active) {
return _indexDataSource.count;
}else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (!self.searchController.active) {
return [_allDataSource[section] count];
}else {
return _searchDataSource.count;
}
}
//頭部索引標(biāo)題(根據(jù)名稱首字母進(jìn)行排序)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (!self.searchController.active) {
return _indexDataSource[section];
}else {
return nil;
}
}
//右側(cè)索引列表(與左側(cè)的數(shù)據(jù)一一對應(yīng))
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if (!self.searchController.active) {
return _indexDataSource;
}else {
return nil;
}
}
//在這里我用的是系統(tǒng)自帶cell,一個(gè)簡單的demo
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
if (!self.searchController.active) {
cell.textLabel.text = _allDataSource[indexPath.section][indexPath.row];
}else{
cell.textLabel.text = _searchDataSource[indexPath.row];
}
return cell;
}
//右邊索引條的點(diǎn)擊事件(點(diǎn)擊每個(gè)字母通訊錄都會跳到相應(yīng)的位置)
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];
return index;
}
pragma mark - UISearchDelegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
[_searchDataSource removeAllObjects];
NSArray *ary = [ZYPinYinSearch searchWithOriginalArray:_dataSource andSearchText:searchController.searchBar.text andSearchByPropertyName:@"name"];
if (searchController.searchBar.text.length == 0) {
[_searchDataSource addObjectsFromArray:_dataSource];
}else {
[_searchDataSource addObjectsFromArray:ary];
}
[self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",cell.textLabel.text);
}
// 懶加載
-(UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)];
self.tableView.delegate = self ;
self.tableView.dataSource = self ;
[self.view addSubview:_tableView];
}
return _tableView ;
}
@end
一個(gè)簡單的通訊錄demo,希望對大家有所幫助,如果需要完整代碼請移步github:https://github.com/Gang679/GZAddressBooks
喜歡的可以star一下,謝謝大家!