给 TableView 加索引的方法不必多说,但有的 TableView 会显示其指向内容为空的索引,如官方自带的通讯录这样:
所谓的非空索引指的是当索引所指向的 section 内容为空时,不添加这条索引,如图:
话不多说,上代码:
省份的模型:
@interface State : NSObject
@property(nonatomic,copy) NSString *name;
@property NSInteger sectionNumber;
@end
加载 plist 中的内容,将其转换为一个二维数组,并在其中用官方的 UILocalizedIndexedCollation 将数组按拼音的字母顺序排序,这里只添加不为空的数组,保证索引都不为空。
- (NSMutableArray *)states
{
if (_states == nil) {
_states = [NSMutableArray arrayWithCapacity:1];
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"ProvincesAndCities" ofType:@"plist"];
NSArray *tempArray;
NSMutableArray *statesTemp;
if (thePath && (tempArray = [NSArray arrayWithContentsOfFile:thePath])) {
statesTemp = [NSMutableArray arrayWithCapacity:1];
for (NSDictionary *stateDict in tempArray) {
State *aState = [[State alloc] init];
aState.name = [stateDict objectForKey:@"State"];
[statesTemp addObject:aState];
}
}else{
return nil;
}
UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];
//1. 将 state 按其属性 name 的拼音顺序排序
for (State *theState in statesTemp) {
NSInteger sect = [theCollation sectionForObject:theState collationStringSelector:@selector(name)];
theState.sectionNumber = sect;
}
//2. 创建一个二维数组
NSInteger highSection = [[theCollation sectionTitles] count];
NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection];
for (int i = 0; i < highSection; i++) {
NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
[sectionArrays addObject:sectionArray];
}
//3. 将 state 按序号加入组中
for (State *theState in statesTemp) {
[(NSMutableArray *)[sectionArrays objectAtIndex:theState.sectionNumber] addObject:theState];
}
//4. 组内排序
for (NSMutableArray *sectionArray in sectionArrays) {
if ([sectionArray count] > 0) {
NSArray *sortedSection = [theCollation sortedArrayFromArray:sectionArray
collationStringSelector:@selector(name)];
[_states addObject:sortedSection];
}
}
}
return _states;
}
UILocalizedIndexedCollation 默认从0开始,指向从A到Z的数据,之前删除掉了为空的字母,这里需要重新将字母顺序与字母对应起来。
- (NSMutableArray *)letterArray
{
if (_letterArray == nil) {
_letterArray = [NSMutableArray arrayWithCapacity:[self.states count]];
NSArray *letterArr = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",
@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",
@"U",@"V",@"W",@"X",@"Y",@"Z"];
for (NSMutableArray *array in self.states) {
if ([array count] > 0) {
State *tmpState = array[0];
[_letterArray addObject:[letterArr objectAtIndex:tmpState.sectionNumber]];
}
}
}
return _letterArray;
}
TableView 的代理方法:
//返回索引数组
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.letterArray;
}
//当点击索引时,跳转到对应的section
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
//返回 section 标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.letterArray objectAtIndex:section];
}