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 {
[super viewDidLoad];
//創建對象
// self.collation = [UILocalizedIndexedCollation currentCollation];
//拿到列表 A - Z 在加一個 # 一共 27個
// NSArray *temp = [self.collation sectionTitles];
// NSLog(@"%@",temp);
[self configureSections];
}
#define NEW_USER(str) [[CKUser alloc] initWith:str name:str]
//配置分組信息
- (void)configureSections {
//初始化測試數據
self.userArray = [[NSMutableArray alloc] init];
[self.userArray addObject:NEW_USER(@"test001")];
[self.userArray addObject:NEW_USER(@"test002")];
[self.userArray addObject:NEW_USER(@"test003")];
[self.userArray addObject:NEW_USER(@"test004")];
[self.userArray addObject:NEW_USER(@"test005")];
[self.userArray addObject:NEW_USER(@"admin01")];
[self.userArray addObject:NEW_USER(@"admin02")];
[self.userArray addObject:NEW_USER(@"admin03")];
[self.userArray addObject:NEW_USER(@"bigBnag01")];
[self.userArray addObject:NEW_USER(@"bigBnag02")];
[self.userArray addObject:NEW_USER(@"what01")];
[self.userArray addObject:NEW_USER(@"what02")];
[self.userArray addObject:NEW_USER(@"李一")];
[self.userArray addObject:NEW_USER(@"李二")];
[self.userArray addObject:NEW_USER(@"劉濤")];
[self.userArray addObject:NEW_USER(@"呂洞賓")];
[self.userArray addObject:NEW_USER(@"劉麻子")];
[self.userArray addObject:NEW_USER(@"曹操")];
[self.userArray addObject:NEW_USER(@"曹值")];
[self.userArray addObject:NEW_USER(@"大海")];
[self.userArray addObject:NEW_USER(@"胡八一")];
[self.userArray addObject:NEW_USER(@"胡八二")];
[self.userArray addObject:NEW_USER(@"小三爺")];
[self.userArray addObject:NEW_USER(@"週記包子")];
//獲得當前UILocalizedIndexedCollation對象並且引用賦給collation,A-Z的數據
self.collation = [UILocalizedIndexedCollation currentCollation];
//獲得索引數和section標題數
NSInteger index, sectionTitlesCount = [[self.collation sectionTitles] count];
//臨時數據,存放section對應的userObjs數組數據
//容量爲 27個
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
//設置sections數組初始化:元素包含userObjs數據的空數據
//往 newSectionsArray 裏面添加了27個空數組
for (index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[newSectionsArray addObject:array];
}
//將用戶數據進行分類,存儲到對應的sesion數組中
//獲取每一個 數據模型
for (CKUser *userObj in self.userArray) {
//根據timezone的localename,獲得對應的的section number
//判斷首字符 的 一個位置
NSInteger sectionNumber = [self.collation sectionForObject:userObj collationStringSelector:@selector(username)];
//獲得section的數組
NSMutableArray *sectionUserObjs = [newSectionsArray objectAtIndex:sectionNumber];
//添加內容到section中
[sectionUserObjs addObject:userObj];
//拿到當前 obj 所在的組數
NSInteger index = [self.collation sectionForObject:userObj collationStringSelector:@selector(username)];
}
//排序,對每個已經分類的數組中的數據進行排序,如果僅僅只是分類的話可以不用這步
for (index = 0; index < sectionTitlesCount; index++) {
//獲取每個數組
NSMutableArray *userObjsArrayForSection = [newSectionsArray objectAtIndex:index];
//獲得排序結果
NSArray *sortedUserObjsArrayForSection = [self.collation sortedArrayFromArray:userObjsArrayForSection collationStringSelector:@selector(username)];
//替換原來數組
[newSectionsArray replaceObjectAtIndex:index withObject:sortedUserObjsArrayForSection];
}
self.sectionsArray = newSectionsArray;
}
//設置Section的數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.collation sectionTitles] count];
}
//設置每個Section下面的cell數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 在該節的時間區域的數目該節數組中的節相關聯的數組的計數
NSArray *UserObjsInSection = [self.sectionsArray objectAtIndex:section];
return [UserObjsInSection count];
}
//設置每行的cell的內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
}
// 從數組中的節索引關聯的數組中獲取區域。
NSArray *userNameInSection = [self.sectionsArray objectAtIndex:indexPath.section];
// 用時區的名稱配置單元格
CKUser *userObj = [userNameInSection objectAtIndex:indexPath.row];
cell.textLabel.text = userObj.username;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/*
* 跟section有關的設定
*/
//設置section的Header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *UserObjsInSection = [self.sectionsArray objectAtIndex:section];
if(UserObjsInSection == nil || [UserObjsInSection count] <= 0) {
return nil;
}
return [[self.collation sectionTitles] objectAtIndex:section];
}
//設置索引標題
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self.collation sectionIndexTitles];
}
//關聯搜索-點擊跳轉到對應組
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
//返回點擊的 頭部 索引
return [self.collation sectionForSectionIndexTitleAtIndex:index];
}
@end