檢索
首先我們先要了解如何實(shí)現(xiàn)檢索,OC有源生的檢索API,使用起來(lái)也相當(dāng)?shù)暮?jiǎn)單方便。只要實(shí)現(xiàn)UITableView的兩個(gè)代理方法即可:
- (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
這個(gè)方法返回檢索字母數(shù)組,我們看看官方文檔:
image
大致意思就是讓你返回一個(gè)字符數(shù)組,右側(cè)檢索條會(huì)顯示你這個(gè)數(shù)組中的字符。
還有一個(gè)方法:
- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index;
這個(gè)方法是一個(gè)事件回調(diào),返回你點(diǎn)擊檢索欄的字符和其在數(shù)組中的下標(biāo)。
如果要展示列表字母頭:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
當(dāng)然UITableView要設(shè)置為group模式。
如果要修改檢索欄顏色:
tableView.sectionIndexColor = [UIColor blueColor];
效果圖
image
檢索數(shù)據(jù)的篩選排序
當(dāng)然,很可能我們從后臺(tái)拿到的數(shù)據(jù)是無(wú)序的,那么就要我們客戶端這里做篩選和排序了。
這里我還是用OC源生的API接口來(lái)實(shí)現(xiàn):UILocalizedIndexedCollation。
這里需要的是一個(gè)二維數(shù)組,所以就自制的一個(gè),并做了個(gè)簡(jiǎn)單的封裝,廢話不多說(shuō),直接上代碼:
#pragma mark - 數(shù)據(jù)分類、排序,array:模型數(shù)組
- (void)classAndCompareWithArray:(NSMutableArray *)array{
UILocalizedIndexedCollation *localizedCollection = [UILocalizedIndexedCollation currentCollation];
//得出collation索引的數(shù)量,這里是27個(gè)(26個(gè)字母和1個(gè)#)
NSInteger sectionTitlesCount = [localizedCollection sectionTitles].count;
//初始化一個(gè)數(shù)組newSectionsArray用來(lái)存放最終的數(shù)據(jù)
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
//初始化27個(gè)空數(shù)組加入newSectionsArray
for (NSInteger index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *arrM = [NSMutableArray array];
[newSectionsArray addObject:arrM];
}
//將每個(gè)地區(qū)按name分到某個(gè)section下
for (CircleListModel *model in array) {
//獲取name屬性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就為11
NSInteger sectionNumber = [localizedCollection sectionForObject:model collationStringSelector:@selector(name)];
NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
[sectionNames addObject:model];
}
//獲取A - # 27個(gè)字符串
NSMutableArray *charactorTitleArrM = [NSMutableArray arrayWithArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
//對(duì)每個(gè)section中的數(shù)組按照name屬性排序
NSMutableArray *emptyCharactorArrM = [NSMutableArray array];
NSMutableArray *emptySectionArrM = [NSMutableArray array];
for (int index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *sectionArrM = newSectionsArray[index];
NSArray *sortedSectionArr = [localizedCollection sortedArrayFromArray:sectionArrM collationStringSelector:@selector(name)];
newSectionsArray[index] = sortedSectionArr;
//記錄空de數(shù)組的下標(biāo)所對(duì)應(yīng)的數(shù)據(jù)
if (sortedSectionArr.count == 0 || sortedSectionArr == nil){
// NSLog(@"aaa--%d",index);
[emptyCharactorArrM addObject:charactorTitleArrM[index]];
[emptySectionArrM addObject:newSectionsArray[index]];
}
}
//刪除空的數(shù)組
// for (NSString *charactor in emptyCharactorArrM) {
// //檢索不展示沒(méi)有數(shù)據(jù)的字母
// [charactorTitleArrM removeObject:charactor];
// }
// for (NSArray *sectionArr in emptySectionArrM) {
//
// [newSectionsArray removeObject:sectionArr];
// }
// //刪除空數(shù)組
// for (int i=0; i<newSectionsArray.count; i++) {
//
// if ([newSectionsArray[i] isKindOfClass:[NSMutableArray class]]){
//
// NSLog(@"a---%d",i);
// NSMutableArray *arrM = newSectionsArray[I];
// if (arrM.count == 0){
//
// NSLog(@"a-a-a-%d",i);
// [charactorTitleArrM removeObjectAtIndex:i];
// [newSectionsArray removeObject:arrM];
// }
// }else if ([newSectionsArray[i] isKindOfClass:[NSArray class]]){
//
// NSLog(@"b---%d",i);
// NSArray *arr = newSectionsArray[I];
// if (arr.count == 0){
//
// NSLog(@"b-b-b-%d",i);
// [charactorTitleArrM removeObjectAtIndex:i];
// [newSectionsArray removeObject:arr];
// }
// }else{
//
// NSLog(@"c---%d",i);
// [charactorTitleArrM removeObjectAtIndex:i];
// [newSectionsArray removeObjectAtIndex:i];
// }
//
// }
//檢索欄
self.charactorTitleArr = charactorTitleArrM.copy;
//最終二維數(shù)組
self.joinScetionArr = newSectionsArray.copy;
}
如果不需要顯示空的數(shù)組和檢索字符,就打開(kāi)注釋部分。
結(jié)語(yǔ)
· 純自己手動(dòng)實(shí)現(xiàn)。