给 TableView 添加非空的索引

给 TableView 加索引的方法不必多说,但有的 TableView 会显示其指向内容为空的索引,如官方自带的通讯录这样:


C、D、E等字母所指向的联系人为空,但是也显示了出来

所谓的非空索引指的是当索引所指向的 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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • __block和__weak修饰符的区别其实是挺明显的:1.__block不管是ARC还是MRC模式下都可以使用,...
    LZM轮回阅读 8,731评论 0 6
  • 给 TableView 加索引的方法不必多说,但有的 TableView 会显示其指向内容为空的索引,如官方自带的...
    搬砖行家阅读 3,308评论 0 1
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,205评论 4 61
  • 作者:史久爱 五月,康乃馨浓情绽放的季节。走在街头,附近花店里飘荡的康乃馨特有的芳香,让我不觉想起了十年前那件令我...
    北方农村阅读 3,106评论 0 0
  • 给自己定下一周看三部电影的目标,今天看《喜剧之王》,对于电影除了表达星爷和柳飘飘之间的爱情主线以外,我更关注的可能...
    阿灵读书阅读 3,759评论 0 0