输入英文数组,建立字典,key是索引数组,value是索引对应的数组。需要提前对数组进行排序,方便后续的分组。取出key数组作为index,
取出vale作为section,生成索引。
//
// ViewController.m
// Indexes
//
// Created by mac on 2017/8/8.
// Copyright © 2017年 mac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) UITableView* tableView;
@property(nonatomic,strong) NSArray* photoes;
@property(nonatomic,strong) NSMutableArray* arrays;
@property(nonatomic,strong) NSArray* indexs;
@property(nonatomic,assign) UIEdgeInsets insets;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
_insets = UIEdgeInsetsMake(0, 15, 0, 0);
NSArray* aa= [[NSArray alloc] init];
aa = @[@"ab",@"ac",@"fg",@"cd",@"ce",@"dd",@"fb",@"daaa",@"zee",@"bvd",@"c22",@"h",@"dd",@"fb",@"daaa",@"zee",@"bvd"];
[self change:aa];
// for(char c = 'A'; c <= 'F'; c++ )
// {
// [_indexs addObject:[NSString stringWithFormat:@"%c",c]];
// }
// _arrays = @[
// @[@"ABC",@"aef",@"aLM",@"aPQ"],
// @[@"bBC",@"bef",@"bLM",@"bPQ"],
// @[@"cBC",@"cef",@"cLM",@"cPQ"],
// @[@"DBC",@"Def",@"DLM",@"DPQ"],
// @[@"EBC",@"Eef",@"ELM",@"EPQ"],
// @[@"FBC",@"Fef",@"FLM",@"FPQ"],
// @[@"GBC",@"Gef",@"GLM",@"GPQ"],];
_photoes =@[@"ic_home_gas",@"ic_home_penalty",@"ic_home_elect",@"ic_home_wallet"];
[self initMyTableView];
}
//数组进行处理
-(NSArray*)change:(NSArray*)aa{
NSArray* test = [[NSArray alloc]init];
_arrays = [[NSMutableArray alloc] init];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
test = [aa sortedArrayUsingSelector:@selector(compare:)];
for(NSInteger i =0;i<test.count;i++){
NSString* first = [[test[i] uppercaseString] substringToIndex:1];
NSMutableArray* array = [dic objectForKey:first];
if(array == nil){
array = [NSMutableArray array];
[dic setObject:array forKey:first];
[array addObject:test[i]];
}else{
[array addObject:test[i]];
}
}
_indexs = [[dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
for(NSInteger a=0;a<_indexs.count;a++){
[_arrays addObject:[dic objectForKey:_indexs[a]]];
}
return _arrays;
}
//初始化UITableView
- (void)initMyTableView{
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
//_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
_tableView.showsVerticalScrollIndicator = NO;
_tableView.sectionIndexColor = [UIColor blueColor];
_tableView.sectionIndexTrackingBackgroundColor = [UIColor clearColor];
_tableView.sectionIndexBackgroundColor = [UIColor clearColor];
//_tableView.separatorStyle = UITableViewCellSelectionStyleNone;;
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
}
//返回section中的row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[_arrays objectAtIndex:section ] count];
return [_photoes count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *Identify = @"CELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identify];
// UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, 2, self.view.frame.size.width-15, 2)];
// line.backgroundColor = [UIColor greenColor];
// [cell.contentView addSubview:line];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identify];
}
//cell.textLabel.text = [_dictionarys objectAtIndex:indexPath.section * 2 + indexPath.row];
cell.textLabel.text = _arrays[indexPath.section][indexPath.row];
UIImage *image = [UIImage imageNamed:_photoes[indexPath.row]];
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, 0.0);
CGRect imageRect = CGRectMake(-6.0, 0.0, itemSize.width, itemSize.height);
[image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return cell;
}
//返回索引数组
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
return _indexs;
}
//响应点击索引时的委托方法
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
NSInteger count = 0;
for (NSString *character in _indexs) {
if ([[character uppercaseString] hasPrefix:title]) {
return count;
}
count++;
}
return 0;
}
//返回每个索引的内容
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [_indexs objectAtIndex:section];
}
//返回section的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [_indexs count];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:_insets];
}
if([cell respondsToSelector:@selector(setLayoutMargins:)]){
[cell setLayoutMargins:_insets];
}
}
@end