使用系统的方法设置,效果图如下:
部分初始化
// 数据源
_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;
}