通訊錄(字母分類)

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


屏幕快照 2016-07-25 11.26.18.png

在這里面要用到兩個(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一下,謝謝大家!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,295評論 3 38
  • 使用Selenium來做自動化測試,一般的流程是: 那么第一步我們需要能夠完成查找并定位元素,Selenium目前...
    Surpassme閱讀 3,173評論 0 3
  • 有多少年是你在望著我的背影目送我走進(jìn)校園走進(jìn)課堂走向未來又有多少年是你在望著我的背影目送我走進(jìn)車站走進(jìn)車廂走向遠(yuǎn)方...
    松月_閱讀 131評論 0 2
  • 乞巧苑中貌不群,碎花窗戶木雕門。 土墻灰瓦廊檐假,橫匾吊屏字跡真。 庭院喧嘩容過客,書屋雅靜了傷痕。 世間仁義知多...
    詩人夏沐閱讀 477評論 0 4
  • 2017.3.31 拿起手機(jī),慣性的寫上記錄的次數(shù),時(shí)間,然后,不知寫些什么好。視線上移,看到記錄的次數(shù),原來時(shí)間...
    薇糖糖糖閱讀 192評論 0 0

友情鏈接更多精彩內(nèi)容