下面來介紹一下UILocalizedIndexedCollation的使用步驟
1、首先新建一個model類,例如:我新建一個Person類,為了簡單點我就添加一個name屬性。回到你的Controller里面進行控件初始化,這里就不多說了。
2、初始化數據源數組,例如:初始化存儲索引標題的數組、每個索引下的section的數組、數據源數組,具體見下圖。
本地新建一個數組作為數據源,例如: ?NSArray *localArray = @[@"林雨", @"林宏偉", @"周董", @"周樹人", @"周杰", @"阿華", @"阿梨", @"安冬", @"楊國棟", @"趙華", @"葉青", @"王曉敏", @"黨國傾", @"吳曉慧", @"任年華", @"陳庚", @"付海波", @"迪拜", @"滴華", @"鶴慶", @"杰斯", @"杰斌", @"牛羊"];
然后for循環添加并賦值Person類的name屬性保存到data數組里,例如下面代碼:
for (int i=0; i<localArray.count;i++) {
? ? ? ? ? Person *model = [Person new];
? ? ? ? ?model.name = localArray[i];
? ? ? ? ? [self.dataArray addObject:model];
}
3、下面開始對UILocalizedIndexedCollation進行初始化。
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
//? ? 獲取collation的索引
NSUInteger sectionNumb = [[collation sectionTitles] count];
NSMutableArray *sectionArray = [[NSMutableArray alloc] init];
//? ? 循環索引,初始化空數組并加入到數據數組
for (NSInteger index = 0; index<sectionNumb;index++){
? ? ? ? ?[sectionArray addObject:[[NSMutableArray alloc] init]];
}
//? ? 1.便利數組中的元素
//? ? 2.獲取name值的首字母在26個字母中所在的位置
//? ? 3.把model對象加到相對應的字母下的數組中去
for(Person *model in self.dataArray){
? ? ? ?NSUInteger sectionIndex = [collation sectionForObject:model ? ? ? ? collationStringSelector:@selector(name)];
? ? ? [sectionArray[sectionIndex] addObject:model];
}
//? ? 對每個section中的數組按照name屬性進行檢索排序(快速索引)
//? ? 1.獲取每個section下的數組
//? ? 2.對每個section下的數組進行字母排序
for(NSInteger index=0; index<sectionNumb;index++){
? ? ? ?NSMutableArray *personArrayForSection = sectionArray[index];
? ? ? ?NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)];
? ? ? ?sectionArray[index] = sortedPersonArrayForSection;
}
//? 新建一個temp空的數組(目的是為了在調用enumerateObjectsUsingBlock函數的時候把空的數組添加到這個數組里,在將數據源空數組移除,或者在函數調用的時候進行判斷,空的移除)
NSMutableArray *temp = [NSMutableArray new];
[sectionArray enumerateObjectsUsingBlock:^(NSArray *arr, NSUInteger idx, BOOL *stop) {
if (arr.count == 0) {
[temp addObject:arr];
} else {
[self.sectionTitlesArray addObject:[collation sectionTitles][idx]];
}
}];
[sectionArray removeObjectsInArray:temp];
self.sectionArray = sectionArray;
4、代理事項
#pragma mark 檢索返回標題數組個數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
? ? ? ? return self.sectionTitlesArray.count;
}
#pragma mark 返回每組下有多少條數據
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
? ? ? ? ?return [self.sectionArray[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
NSUInteger section = indexPath.section;
NSUInteger row = indexPath.row;
Person *model = self.sectionArray[section][row];
cell.textLabel.text = model.name;
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
// 按順序顯示tableview的HeaderInSection標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{? ?
? ? ? return [self.sectionTitlesArray objectAtIndex:section];
}
// 按順序顯示檢索字母(不返回就不顯示右邊索引內容)
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
? ? ? ?return self.sectionTitlesArray;
}
//? 點擊右邊索引執行的代理方法,可以在這里設置顯示手指滑動時候索引的標題(sectionIndexColor是設置字體顏色,sectionIndexBackgroundColor背景顏色)
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
? ? self.titleLabel.hidden = NO;
? ? ?self.titleLabel.text = title;
? ? NSString *key = [self.sectionTitlesArray objectAtIndex:index];
? ? for (UIView *view in tableView.subviews) {
? ? ? ? NSLog(@"_________%@",view);
? ? ? if ([view isKindOfClass:NSClassFromString(@"UITableViewIndex")]) {
? ? }
}
? ? ?return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
- (CGFloat)tableView:(UITableView *)tableView? heightForRowAtIndexPath:(NSIndexPath *)indexPath{
? ? ?return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
? ? ?return 35;
}
做完這些就已經完成UILocalizedIndexedCollation智能分組排序了,樣式就如下效果圖。