通讯录


@property (nonatomic, strong) NSArray * tableData;//section数组
@property (nonatomic, strong) NSMutableArray * resultData;//搜索后的section数组
@property (nonatomic, strong) NSArray * tableIndexData;//cell数据
@property (nonatomic, strong) NSMutableArray * resultIndexData;//搜索后的cell数组
/** 列表 */
@property (nonatomic , strong) UITableView *contactTableView;

@property (nonatomic, assign) BOOL  searchActive;
- (void) initializeDataSource;
- (void) initializeInterface;
@end

@implementation ContactsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.customNavBar.title = @"人脉";
    [self.customNavBar wr_setLeftButtonWithImage:nil];  
    [self.customNavBar wr_setRightButtonWithTitle:@"添加" titleColor:[UIColor colorWithHexString:@"#ffffff"]];
    [self initializeDataSource];
    [self initializeInterface];
}
- (void) initializeDataSource{
    NSArray *testArr = @[@{@"name":@"Ives Leo",@"job":@"西物吉利·销售"},@{@"name":@"李逍遥",@"job":@"西物吉利·店长"},@{@"name":@"王宝强",@"job":@"西物吉利·接待"},@{@"name":@"赵敏",@"job":@"西物吉利·财务"},@{@"name":@"阿刁",@"job":@"西物吉利·财务经理"},@{@"name":@"王小二",@"job":@"西物吉利·销售主管"},@{@"name":@"秦明",@"job":@"西物吉利·店长助理"},@{@"name":@"刘放",@"job":@"爱车保·产品经理"}];
    NSMutableArray * personArray = [NSMutableArray arrayWithCapacity:testArr.count];
    for (int i = 0; i < testArr.count; i ++) {
        ICBContactModel * model = [ICBContactModel new];
        model.nameString = testArr[i][@"name"];
        model.workNameString = testArr[i][@"job"];
        model.headImage = [self getNameImage:testArr[i][@"name"]];
        [personArray addObject:model];
    }
    NSArray * tempArray = [self sringSectioncompositor:personArray withSelector:@selector(nameString)isDeleEmptyArray:YES];
    self.tableData = tempArray[0];
    self.tableIndexData = tempArray[1];
}
- (UIImage *)getNameImage:(NSString *)name {
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];
    lab.layer.cornerRadius = CGRectGetWidth(lab.frame)/2.;
    lab.backgroundColor = [UIColor colorWithHexString:@"#f04848"];
    lab.textColor = [UIColor whiteColor];
    lab.font = [UIFont systemFontOfSize:18];
    lab.textAlignment = NSTextAlignmentCenter;
    if ([self hasChinese:name]) {
        if (name.length > 1) {
            lab.text = [name substringFromIndex:name.length-2];
        }else{
            lab.text = name;
        }
    }else {
        NSArray  *strs = [name componentsSeparatedByString:@" "];
        if (strs.count > 1) {
            lab.text = [NSString stringWithFormat:@"%@%@",[strs[0] substringWithRange:NSMakeRange(0, 1)],[strs[1] substringWithRange:NSMakeRange(0, 1)]];
        }else {
            lab.text = [NSString stringWithFormat:@"%@",[strs[0] substringWithRange:NSMakeRange(0, 1)]];
        }
    }
    return [UIView imageFromView:lab];
}
-(BOOL)hasChinese:(NSString *)str {
    for(int i=0; i< [str length];i++){
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)
        {
            return YES;
        }
    }
    return NO;
}
- (void) initializeInterface{
    self.view.backgroundColor = [UIColor whiteColor];
    AdjustsScrollViewInsetNever(self, self.contactTableView)
    [self.view insertSubview:self.contactTableView belowSubview:self.customNavBar];
    //注册
    [self.contactTableView registerNib:[UINib nibWithNibName:@"ICBContactTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
    //搜索框
    UISearchBar * mSearchBar = [[UISearchBar alloc] init];
    [mSearchBar sizeToFit];
    mSearchBar.delegate = self;
    [mSearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    mSearchBar.searchBarStyle = UISearchBarStyleMinimal;
    mSearchBar.placeholder = @"搜索";
    self.contactTableView.tableHeaderView = mSearchBar;
    self.contactTableView.sectionIndexColor = [[UIColor grayColor] colorWithAlphaComponent:0.3];
}
#pragma mark --------------init------------------
//将汉字转为拼音 是否支持全拼可选
- (NSString *)transformToPinyin:(NSString *)aString isQuanPin:(BOOL)quanPin
{
    //转成了可变字符串
    NSMutableString *str = [NSMutableString stringWithString:aString];
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin,NO);
    
    //再转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics,NO);
    NSArray *pinyinArray = [str componentsSeparatedByString:@" "];
    NSMutableString *allString = [NSMutableString new];
    
    if (quanPin)
    {
        int count = 0;
        for (int  i = 0; i < pinyinArray.count; i++)
        {
            for(int i = 0; i < pinyinArray.count;i++)
            {
                if (i == count) {
                    [allString appendString:@"#"];
                    //区分第几个字母
                }
                [allString appendFormat:@"%@",pinyinArray[i]];
            }
            [allString appendString:@","];
            count ++;
        }
    }
    
    NSMutableString *initialStr = [NSMutableString new];
    //拼音首字母
    for (NSString *s in pinyinArray)
    {
        if (s.length > 0)
        {
            [initialStr appendString:[s substringToIndex:1]];
        }
    }
    [allString appendFormat:@"#%@",initialStr];
    [allString appendFormat:@",#%@",aString];
    return allString;
}

//将传进来的对象按通讯录那样分组排序,每个section中也排序  dataarray是中存储的是一组对象,selector是属性名
- (NSArray *)sringSectioncompositor:(NSArray *)dataArray withSelector:(SEL)selector isDeleEmptyArray:(BOOL)isDele
{
    
    //    UILocalizedIndexedCollation是苹果贴心为开发者提供的排序工具,会自动根据不同地区生成索引标题
    UILocalizedIndexedCollation  *collation = [UILocalizedIndexedCollation currentCollation];
    
    NSMutableArray * indexArray = [NSMutableArray arrayWithArray:collation.sectionTitles];
    
    NSUInteger sectionNumber = indexArray.count;
    //建立每个section数组
    NSMutableArray * sectionArray = [NSMutableArray arrayWithCapacity:sectionNumber];
    for (int n = 0; n < sectionNumber; n++)
    {
        NSMutableArray *subArray = [NSMutableArray array];
        [sectionArray addObject:subArray];
    }
    
    for (ICBContactModel *model in dataArray)
    {
        //        根据SEL方法返回的字符串判断对象应该处于哪个分区
        NSInteger index = [collation sectionForObject:model collationStringSelector:selector];
        NSMutableArray *tempArray = sectionArray[index];
        [tempArray addObject:model];
    }
    for (NSMutableArray *tempArray in sectionArray)
    {
        //        根据SEL方法返回的string对数组元素排序
        NSArray* sorArray = [collation sortedArrayFromArray:tempArray collationStringSelector:selector];
        [tempArray removeAllObjects];
        [tempArray addObjectsFromArray:sorArray];
    }
    
    //    是否删除空数组
    if (isDele)
    {
        [sectionArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSArray *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.count == 0)
            {
                [sectionArray removeObjectAtIndex:idx];
                [indexArray removeObjectAtIndex:idx];
            }
        }];
    }
    //返回第一个数组为table数据源  第二个数组为索引数组
    return @[sectionArray, indexArray];
}
#pragma mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //加个多线程,否则数量量大的时候,有明显的卡顿现象
    //这里最好放在数据库里面再进行搜索,效率会更快一些
    if (searchText.length == 0)
    {
        _searchActive = NO;
        [self.contactTableView reloadData];
        return;
    }
    _searchActive = YES;
    _resultData = [NSMutableArray array];
    _resultIndexData = [NSMutableArray array];
    
    dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
    dispatch_async(globalQueue, ^{
        //遍历需要搜索的所有内容,其中self.dataArray为存放总数据的数组
        [self.tableData enumerateObjectsUsingBlock:^(NSMutableArray * obj, NSUInteger aIdx, BOOL * _Nonnull stop) {
            //刚进来 && 第一个数组不为空时 插入一个数组在第一个位置
            if (_resultData.count == 0 || [[_resultData lastObject] count] != 0)
            {
                [_resultData addObject:[NSMutableArray array]];
            }
            
            [obj enumerateObjectsUsingBlock:^(ICBContactModel * model, NSUInteger bIdx, BOOL * _Nonnull stop) {
                
                NSString *tempStr = model.nameString;
                //----------->把所有的搜索结果转成成拼音
                NSString *pinyin = [self transformToPinyin:tempStr isQuanPin:NO];
                
                if ([pinyin rangeOfString:searchText options:NSCaseInsensitiveSearch].length > 0)
                {
                    //把搜索结果存放self.resultArray数组
                    [_resultData.lastObject addObject:model];
                    if (_resultIndexData == 0 || ![_resultIndexData.lastObject isEqualToString:_tableIndexData[aIdx]])
                    {
                        [_resultIndexData addObject:_tableIndexData[aIdx]];
                    }
                }
            }];
        }];
        //回到主线程
        if ([_resultData.lastObject count] == 0)
        {
            [_resultData removeLastObject];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.contactTableView reloadData];
        });
    });
}
#pragma mark - Scroll View Delegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
#pragma mark --------------UITableViewDataSource,UITableViewDelegate------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _searchActive ? _resultData.count : _tableData.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _searchActive ? [_resultData[section] count] : [_tableData[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICBContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ICBContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    ICBContactModel *model = _searchActive ? _resultData[indexPath.section][indexPath.row] : _tableData[indexPath.section][indexPath.row];
    cell.model = model;
    return cell;
}

- (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return _searchActive ? _resultIndexData : _tableIndexData;
}

//返回当用户触摸到某个索引标题时列表应该跳至的区域的索引。
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    //    [SVProgressHUD showImage:nil status:title];
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
#pragma mark - Table View Delegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];
    UILabel * tempLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, self.view.bounds.size.width, 20)];
    tempLab.text = _searchActive ? _resultIndexData[section] : _tableIndexData[section];
    tempLab.textColor = [UIColor darkGrayColor];
    tempLab.font = ICBFont(15);
    tempLab.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];
    [headerView addSubview:tempLab];
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark --------------lazy------------------
- (UITableView *)contactTableView{
    if(!_contactTableView){
        _contactTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, ScreenWidth, ScreenHeight - SafeAreaTopHeight - SafeAreaBottomHeight) style:UITableViewStylePlain];
        _contactTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _contactTableView.backgroundColor = [UIColor whiteColor];
        _contactTableView.dataSource = self;
        _contactTableView.delegate = self;
        _contactTableView.rowHeight = 60;
    }
    return _contactTableView;
}
@end

你可能感兴趣的:(通讯录)