美文网首页
tableView 添加索引和根据索引分类

tableView 添加索引和根据索引分类

作者: 330PG | 来源:发表于2019-12-13 18:54 被阅读0次
    
    @interface FZAddressBookListViewController () <UITableViewDelegate,UITableViewDataSource>
    
    @property(nonatomic,strong)UITableView *tableView;
    @property(nonatomic,strong)NSMutableArray *dataArray; //请求到的数据
    @property(nonatomic,strong)NSMutableArray *indexTitles; //索引title
    @property(nonatomic,strong)NSMutableDictionary *groupListDic; //索引对应数据 
    @end
    
    @implementation FZAddressBookListViewController
    {
        MBProgressHUD *hud;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        [self configUI];
        [self getListData];
    }
    
    -(void)configUI{
        _tableView = [[UITableView alloc]init];
        _tableView.sectionIndexColor = [UIColor blackColor];
        _tableView.tableFooterView=[[UIView alloc]init];
        _tableView.sectionIndexBackgroundColor=[UIColor clearColor];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view);
            make.left.mas_equalTo(self.view);
            make.size.mas_equalTo(self.view);
        }];
        [MBProgressHUD showMessage:@"" toView:self.view];
    }
    
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 70;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        
        if (self.indexTitles.count > 0) {
            NSString *key=self.indexTitles[section];
            NSMutableArray *keyArray = [self.groupListDic objectForKey:key];
            return keyArray.count;
        }
        return  0;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        FZAddressBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"celladd"];
        if (!cell) {
            cell = [[FZAddressBookTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"celladd"];
        }
        
        if (self.dataArray.count > 0) {
            
            NSString *key=self.indexTitles[indexPath.section];
            NSMutableArray *keyArray = [self.groupListDic objectForKey:key];
            cell.model=(FZAddressUserModel *)[keyArray objectAtIndex:indexPath.row];
        }
        return  cell;
        
    }
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return self.indexTitles.count;
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 25;
    }
    
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        UIView *bgView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 25)];
        bgView.backgroundColor=[UIColor colorWithHexString:@"EAEAEA"];
        FZLabel *lab =[[FZLabel alloc]initWithTextColorHex:@"666666" font:13 textAlignment:NSTextAlignmentLeft];
        [bgView addSubview:lab];
        lab.frame=CGRectMake(20, 0, 90, 25);
        if (self.indexTitles.count > 0) {
            lab.text =(NSString *) self.indexTitles[section];
        }
        return bgView;
    }
    
    - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView; {
        if (self.indexTitles.count > 0) {
            return self.indexTitles;
        }
        return nil;
    }
    
    -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index{
        NSLog(@"title,index %@,%ld",title,index);
        return index;
    }
    
    -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
        for (UIView *view in [tableView subviews]) {
            if ([view isKindOfClass:[NSClassFromString(@"UITableViewIndex") class]]) {
                
    
               // 设置字体大小
                [view setValue:[UIFont systemFontOfSize:15.0 weight:UIFontWeightRegular] forKey:@"_font"];
                
                [view setValue:[UIColor colorWithHexString:@"1E90FF"] forKey:@"_indexColor"];
                
               //设置view的大小
                view.bounds = CGRectMake(2, 0, 30, 30);
                //单单设置其中一个是无效的
            }
        }
    }
    
    
    #pragma mark --- 数据请求
    
    -(void)getListData{
        [FZRequestTools getStudentsByTeacherId:[FZUserInfo sharedUtil].colUid handler:^(BOOL success, NSString *msg, id response) {
            if (success) {
                  NSArray *responseArray=[response objectForKey:@"response"];
                  NSMutableArray *modelArray=[[NSMutableArray alloc]init];
                  NSMutableArray *allFirstLetters=[[NSMutableArray alloc]init];
                  for (NSDictionary *dic in responseArray) {
                    FZAddressUserModel *model=[FZAddressUserModel mj_objectWithKeyValues:dic];
                      [modelArray addObject:model];
                      
                      NSString *firstLetter = [self firstCharactor:model.userName];
                      [allFirstLetters addObject:firstLetter];
                  }
                 allFirstLetters = [allFirstLetters valueForKeyPath:@"@distinctUnionOfObjects.self"];
                
                 self.indexTitles= [NSMutableArray arrayWithArray:[allFirstLetters sortedArrayUsingSelector:@selector(compare:)]];
                 [self.dataArray addObjectsFromArray:modelArray];
                 [self sortedResult];
                 [self->_tableView reloadData];
                 [MBProgressHUD hideHUDForView:self.view];
            }else{
                
            }
        }];
    }
    
    #pragma mark -- suoyin
    
    -(void)sortedResult{
        
        for (NSString *keyLetter in self.indexTitles) {
            NSMutableArray *tempArray =[[NSMutableArray alloc]init];
            [self.groupListDic setObject:tempArray forKey:keyLetter];
        }
        
            
             
        for (NSString *letter in self.indexTitles) {
                  
            NSMutableArray *tempkArray =[[NSMutableArray alloc]init];
            tempkArray = [self.groupListDic objectForKey:letter];
               
            for (FZAddressUserModel *uModel in self.dataArray) {
                if ([[self firstCharactor:uModel.userName] isEqualToString:letter]) {
                    [tempkArray addObject:uModel];
                }
            }
            [self.groupListDic setObject:tempkArray forKey:letter];
        }
    }
    
    - (NSString *)firstCharactor:(NSString *)aString
    
    {
        //转成了可变字符串
        NSMutableString *str = [NSMutableString stringWithString:aString];
        CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);//先转换为带声调的拼音
        CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);//再转换为不带声调的拼音
        NSString *pinYin = [str capitalizedString];//转化为大写拼音
        
        //获取并返回首字母
        return [pinYin substringToIndex:1];
    }
    
    -(NSMutableArray *)dataArray{
        if (!_dataArray) {
            _dataArray=[[NSMutableArray alloc]init];
        }
        return _dataArray;
    }
    
    -(NSMutableArray *)indexTitles{
        if (!_indexTitles) {
            _indexTitles=[[NSMutableArray alloc]init];
        }
        return _indexTitles;
    }
    
    -(NSMutableDictionary *)groupListDic{
        if (!_groupListDic) {
            _groupListDic=[[NSMutableDictionary alloc]init];
        }
        return _groupListDic;
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:tableView 添加索引和根据索引分类

          本文链接:https://www.haomeiwen.com/subject/cohqnctx.html