1,创建模型:用来保存数据
创建模型:用来保存数据
模型.h文件
@interface CKUser : NSObject
//重写构造方法
-(instancetype)initWith:(NSString*)username name:(NSString*)name;
/*
* 名字
*/
@property (assign,readwrite, nonatomic)NSString *name;
@property (assign,readwrite, nonatomic)NSString *username;
@end
模型.m文件@implementation CKUser
-(instancetype)initWith:(NSString*)username name:(NSString*)name {
self = [super init];
self.username = username;
self.name = name;
return self;
}
@end
根控制器设置UITableViewController的控制器
#import "ViewController.h"
#import "CKUser.h"
@interface ViewController ()
//数据保存的模型数组
@property (nonatomic,strong) NSMutableArray *userArray;
//分组的数组
@property (nonatomic,strong) NSMutableArray *sectionsArray;
//UITableView索引搜索工具类
@property (nonatomic,strong) UILocalizedIndexedCollation *collation;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//创建对象
// self.collation = [UILocalizedIndexedCollation currentCollation];
//拿到列表 A - Z在加一个 # 一共 27个
// NSArray *temp = [self.collation sectionTitles];
// NSLog(@"%@",temp);
[selfconfigureSections];
}
#define NEW_USER(str) [[CKUser alloc] initWith:str name:str]
//配置分组信息
- (void)configureSections {
//初始化测试数据
self.userArray = [[NSMutableArrayalloc] init];
[self.userArrayaddObject:NEW_USER(@"test001")];
[self.userArrayaddObject:NEW_USER(@"test002")];
[self.userArrayaddObject:NEW_USER(@"test003")];
[self.userArrayaddObject:NEW_USER(@"test004")];
[self.userArrayaddObject:NEW_USER(@"test005")];
[self.userArrayaddObject:NEW_USER(@"admin01")];
[self.userArrayaddObject:NEW_USER(@"admin02")];
[self.userArrayaddObject:NEW_USER(@"admin03")];
[self.userArrayaddObject:NEW_USER(@"bigBnag01")];
[self.userArrayaddObject:NEW_USER(@"bigBnag02")];
[self.userArrayaddObject:NEW_USER(@"what01")];
[self.userArrayaddObject:NEW_USER(@"what02")];
[self.userArrayaddObject:NEW_USER(@"李一")];
[self.userArrayaddObject:NEW_USER(@"李二")];
[self.userArrayaddObject:NEW_USER(@"刘涛")];
[self.userArrayaddObject:NEW_USER(@"吕洞宾")];
[self.userArrayaddObject:NEW_USER(@"刘麻子")];
[self.userArrayaddObject:NEW_USER(@"曹操")];
[self.userArrayaddObject:NEW_USER(@"曹值")];
[self.userArrayaddObject:NEW_USER(@"大海")];
[self.userArrayaddObject:NEW_USER(@"胡八一")];
[self.userArrayaddObject:NEW_USER(@"胡八二")];
[self.userArrayaddObject:NEW_USER(@"小三爷")];
[self.userArrayaddObject:NEW_USER(@"周记包子")];
//获得当前UILocalizedIndexedCollation对象并且引用赋给collation,A-Z的数据
self.collation = [UILocalizedIndexedCollationcurrentCollation];
//获得索引数和section标题数
NSInteger index, sectionTitlesCount = [[self.collationsectionTitles] count];
//临时数据,存放section对应的userObjs数组数据
//容量为 27个
NSMutableArray *newSectionsArray = [[NSMutableArrayalloc] initWithCapacity:sectionTitlesCount];
//设置sections数组初始化:元素包含userObjs数据的空数据
//往 newSectionsArray 里面添加了27个空数组
for (index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *array = [[NSMutableArrayalloc] init];
[newSectionsArray addObject:array];
}
//将用户数据进行分类,存储到对应的sesion数组中
//获取每一个数据模型
for (CKUser *userObjin self.userArray) {
//根据timezone的localename,获得对应的的section number
//判断首字符的 一个位置
NSInteger sectionNumber = [self.collationsectionForObject:userObj collationStringSelector:@selector(username)];
//获得section的数组
NSMutableArray *sectionUserObjs = [newSectionsArrayobjectAtIndex:sectionNumber];
//添加内容到section中
[sectionUserObjs addObject:userObj];
//拿到当前 obj所在的组数
NSInteger index = [self.collationsectionForObject:userObj collationStringSelector:@selector(username)];
}
//排序,对每个已经分类的数组中的数据进行排序,如果仅仅只是分类的话可以不用这步
for (index = 0; index < sectionTitlesCount; index++) {
//获取每个数组
NSMutableArray *userObjsArrayForSection = [newSectionsArrayobjectAtIndex:index];
//获得排序结果
NSArray *sortedUserObjsArrayForSection = [self.collationsortedArrayFromArray:userObjsArrayForSection collationStringSelector:@selector(username)];
//替换原来数组
[newSectionsArray replaceObjectAtIndex:indexwithObject:sortedUserObjsArrayForSection];
}
self.sectionsArray = newSectionsArray;
}
//设置Section的数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.collationsectionTitles] count];
}
//设置每个Section下面的cell数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 在该节的时间区域的数目该节数组中的节相关联的数组的计数
NSArray *UserObjsInSection = [self.sectionsArrayobjectAtIndex:section];
return [UserObjsInSection count];
}
//设置每行的cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Identifier =@"Cell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:Identifier];
}
// 从数组中的节索引关联的数组中获取区域。
NSArray *userNameInSection = [self.sectionsArrayobjectAtIndex:indexPath.section];
// 用时区的名称配置单元格
CKUser *userObj = [userNameInSection objectAtIndex:indexPath.row];
cell.textLabel.text = userObj.username;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPathanimated:YES];
}
/*
* 跟section有关的设定
*/
//设置section的Header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *UserObjsInSection = [self.sectionsArrayobjectAtIndex:section];
if(UserObjsInSection == nil || [UserObjsInSection count] <= 0) {
return nil;
}
return [[self.collationsectionTitles] objectAtIndex:section];
}
//设置索引标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self.collationsectionIndexTitles];
}
//关联搜索-点击跳转到对应组
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
//返回点击的头部 索引
return [self.collationsectionForSectionIndexTitleAtIndex:index];
}
@end