UITableView分组索引

使用系统的方法设置,效果图如下:

效果图.png

部分初始化

    // 数据源
    _dataModelArray = [[NSMutableArray alloc] init];
    // 设置索引
    _charArray = [[NSMutableArray alloc] init];
    // 索引字体颜色
    _tableView.sectionIndexColor = [UIColor colorWithHexString:@"686a6c"];
    // 索引背景色
    _tableView.sectionIndexBackgroundColor = [UIColor clearColor];

数据源的添加

                for (char key = 'A'; key<='Z'; key++) {
            
                    NSArray * eachArray = [memberArrayDic objectForKey:[NSString stringWithFormat:@"%c",key]];
                    if (eachArray) {
                        // 添加索引
                        [_charArray addObject:[NSString stringWithFormat:@"%c",key]];
                        
                        NSMutableArray * eachModelArray = [[NSMutableArray alloc] init];
                        for (NSDictionary * dict in eachArray) {
                            GroupUserModel * model = [GroupUserModel yy_modelWithDictionary:dict];
                            [eachModelArray addObject:model];
                        }
                        [_dataModelArray addObject:eachModelArray];
                    }

                }
                
                NSArray * otherArray = memberArrayDic[@"#"];
                if (otherArray) {
                    [_charArray addObject:@"#"];
                    
                    NSMutableArray * otherModelArray = [[NSMutableArray alloc] init];
                    for (NSDictionary * dic in otherArray) {
                        GroupUserModel * model = [GroupUserModel yy_modelWithDictionary:dic];
                        [otherModelArray addObject:model];
                    }
                    
                    [_dataModelArray addObject:otherModelArray];
                }

UITableView设置

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_dataModelArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 22;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_dataModelArray[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * identifier = @"cell";
    GroupSettingUserCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"GroupSettingUserCell" owner:self options:nil].firstObject;
    }
    
    GroupUserModel * model = _dataModelArray[indexPath.section][indexPath.row];
    cell.userName = model.chatName;
    if ([model.selectedType isEqualToString:@"1"]) {
        cell.isSelected = YES;
    } else {
        cell.isSelected = NO;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    GroupUserModel * model = _dataModelArray[indexPath.section][indexPath.row];
    if ([model.selectedType isEqualToString:@"0"]) {
        model.selectedType = @"1";
        _selectedUserCount ++;
    } else {
        model.selectedType = @"0";
        if (_selectedUserCount) {
            _selectedUserCount --;
        }
    }
    _upView.selectedCount = _selectedUserCount;
    [_tableView reloadData];
}
// 索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return _charArray;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    NSLog(@"===%@  ===%ld",title,(long)index);
    
    //点击索引,列表跳转到对应索引的行
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]
                     atScrollPosition:UITableViewScrollPositionTop animated:YES];
    
    return index;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 22)];
    headView.backgroundColor = [UIColor colorWithHexString:@"ebebeb"];

    //标题文字
    UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 40, 22)];
    title.backgroundColor = [UIColor clearColor];
    title.font = [UIFont systemFontOfSize:12];
    title.textColor = [UIColor colorWithHexString:@"a2a4a7"];
    title.text = _charArray[section];
    [headView addSubview:title];
    
    return headView;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,390评论 19 139
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,954评论 3 38
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,205评论 4 61
  • 今天看电视上一个纪录片,采访到某个国企老工人,片子介绍道:这名老工人在某某岗位上12年,把宝贵的青春都奉献给XX事...
    小火咕嘟阅读 3,552评论 0 0
  • 越是难受时,越是需要学习时,学习不一定指看教科书,而是寻找解决问题的方法,更忌自己胡思乱想,沉溺情绪。 要慢慢的让...
    水面清圆一一风荷举苏幕遮阅读 3,142评论 0 0