#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *userArray; //数据源数组
@property (strong,nonatomic) NSDictionary *dicArr;
@property (nonatomic, strong) UITableView *tableView;
//UITableView索引搜索工具
@property (nonatomic,strong) NSMutableArray *keys;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self configureSections];
CGRect frame = self.view.bounds;
self.tableView =
[[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
// Do any additional setup after loading the view, typically from a nib.
}
- (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];
}
//配置分组信息
- (void)configureSections {
//初始化测试数据
self.dicArr = [[NSMutableDictionary alloc] init];
_userArray = [NSMutableArray arrayWithObjects:@"一小",@"二小",@"三小",@"四小",@"五小",@"六小",@"二中",@"三中",@"四中",@"八中",@"一中",@"四高",@"五高",@"一高", nil];
for (NSString *name in _userArray) {
NSString * A= [self firstCharactor:name];
NSMutableArray *mArr=[self.dicArr objectForKey:A];
if (!mArr) {
mArr = [[NSMutableArray alloc]init];
}
[mArr addObject:name];
[self.dicArr setValue:mArr forKey:A];
}
NSArray *keysArray = [self.dicArr allKeys];
NSArray *resultArray = [keysArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
self.keys= [[NSMutableArray alloc]initWithArray: resultArray];
}
#pragma mark -- delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// The number of sections is the same as the number of titles in the collation.
return [self.keys count];
}
//设置每个Section下面的cell数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// The number of time zones in the section is the count of the array associated with the section in the sections array.
return [[self.dicArr objectForKey:[self.keys objectAtIndex:section]] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[self.dicArr objectForKey:[self.keys objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"%@", [[self.dicArr objectForKey:[self.keys objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]);
}
/*
* 跟section有关的设定
*/
//设置section的Header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [self.keys objectAtIndex:section];
}
//设置索引标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.keys;
}
//索引点击事件
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];
return index;
}
@end
tableview 侧边 index
最新推荐文章于 2018-11-21 11:32:25 发布