一.建立person类包含name pinyin参数
#import "Person.h"
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *pinYin;
二.实现类
#import "Person.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
<UITableViewDelegate,UITableViewDataSource>
{NSMutableArray *dataSource;
UITableView *myTableView;
NSMutableArray *titleAray;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"UITableView";
self.view.backgroundColor = [UIColor lightGrayColor];
dataSource = [NSMutableArray array];
NSArray *arr = [[NSArray alloc] initWithObjects:@"王达",@"李明",@"李松",@"朱子夫",@"秦始皇",@"刘彻",@"李世民",@"周树人",@"李松达",@"邓紫棋",@"安美",@"安菲",@"崔杰",@"冰冰",@"啊Q", nil];
NSMutableArray *modelArray = [NSMutableArray array];
for (int i = 0; i< arr.count; i++) {
NSMutableString *mutableString = [NSMutableString stringWithString:arr[i]];
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripDiacritics, false);
Person *model = [[Person alloc] init];
model.name = arr[i];
model.pinYin = mutableString;
[modelArray addObject:model];
}
//NSSortDescriptor 指定用于对象数组排序的对象的属性
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]];
//对person进行排序
[modelArray sortUsingDescriptors:sortDescriptors];
titleAray = [NSMutableArray array];
//删除多余的字符
for (int i = 0; i < modelArray.count; i++) {
Person *model = modelArray[i];
NSString *str = [model.pinYin substringToIndex:1];
if (![titleAray containsObject:str]) {
[titleAray addObject:str];
}
}
//将中文数据进行二维的数组的拆分
for (int i = 0; i< titleAray.count; i++) {
NSString *str = titleAray[i];
NSMutableArray *sortArray = [NSMutableArray array];
for (Person *model in modelArray) {
if ([model.pinYin hasPrefix:str]) {
[sortArray addObject:model.name];
}
}
[dataSource addObject:sortArray];
}
[self creatTableView];
}
#pragma mark - UITableViewDelegate
-(void)creatTableView{
myTableView = [[UITableView alloc] initWithFrame:self.view.frame];
myTableView.delegate = self;
myTableView.dataSource = self;
//设置右侧索引的背景色
myTableView.sectionIndexBackgroundColor = [UIColor clearColor];
[self.view addSubview:myTableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dataSource[section] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 55;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray *toBeReturned = [[NSMutableArray alloc]init];
for(char c = 'A';c<='Z';c++)
[toBeReturned addObject:[NSString stringWithFormat:@"%c",c]];
return toBeReturned;
}
#pragma mark - UITableViewDataSource
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
view.backgroundColor = [UIColor grayColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 60, 20)];
NSString *str = titleAray[section];
//转化为大写
str = [str uppercaseString];
label.text = str;
[view addSubview:label];
return view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
cell.textLabel.text = dataSource[indexPath.section][indexPath.row];
return cell;
}