实现TableView索引功能

UITableView 自带索引,一直用第三方的控件也没有认真研究,今天又碰到了类似问题,于是专门研究了下详细的内容,记录于此。

  • 1.创建TableView
@property (strong, nonatomic) UITableView *tableView;
//需添加代理和数据源
  • 2.随机生成一些基础数据
NSArray *xings = @[@"赵",@"钱",@"孙",@"李",@"周",@"吴",@"郑",@"王",@"冯",@"陈",@"楚",@"卫",@"沈",@"杀",@"韩",@"杨"];
NSArray *ming1 = @[@"大",@"美",@"帅",@"应",@"超",@"海",@"江",@"湖",@"春",@"夏",@"秋",@"冬",@"上",@"左",@"有",@"纯"];
NSArray *ming2 = @[@"强",@"好",@"领",@"亮",@"超",@"华",@"奎",@"海",@"工",@"青",@"红",@"潮",@"兵",@"垂",@"刚",@"山"];
    
    for (int i = 0; i < count; i++) {
        NSString *name = xings[arc4random_uniform((int)xings.count)];
        NSString *ming = ming1[arc4random_uniform((int)ming1.count)];
        name = [name stringByAppendingString:ming];
        if (arc4random_uniform(10) > 3) {
            NSString *ming = ming2[arc4random_uniform((int)ming2.count)];
            name = [name stringByAppendingString:ming];
        }
        //数据模型进行存储
        SDContactModel *model = [SDContactModel new];
        model.name = name;
        model.imageName = [SDAnalogDataGenerator randomIconImageName];
        [self.dataArray addObject:model];
    }
  • 3.关键排序
    _contacts = [self arrayForSections:objects];
    [_tableView reloadData];
- (NSArray *)arrayForSections:(NSArray *)objects {
    SEL selector = @selector(name);
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
    // sectionTitlesCount 27 , A - Z + #
    
    // section number
    NSInteger sectionTitlesCount = [[collation sectionTitles] count];
    
    // Create 27 sections' data
    NSMutableArray *mutableSections = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
    for (NSUInteger idx = 0; idx < sectionTitlesCount; idx++) {
        [mutableSections addObject:[NSMutableArray array]];
    }
    
    // Insert records
    for (id object in objects) {
        NSInteger sectionNumber = [collation sectionForObject:object collationStringSelector:selector];
        [[mutableSections objectAtIndex:sectionNumber] addObject:object];
    }
    
    // sort in section
    for (NSUInteger idx = 0; idx < sectionTitlesCount; idx++) {
        NSArray *objectsForSection = [mutableSections objectAtIndex:idx];
        [mutableSections replaceObjectAtIndex:idx withObject:[[UILocalizedIndexedCollation currentCollation] sortedArrayFromArray:objectsForSection collationStringSelector:selector]];
    }
    
    // Remove empty sections && insert table data
    NSMutableArray *existTitleSections = [NSMutableArray array];
    for (NSArray *section in mutableSections) {
        if ([section count] > 0) {
            [existTitleSections addObject:section];
        }
    }
    
    // Remove empty sections Index in indexList
    NSMutableArray *existTitles = [NSMutableArray array];
    NSArray *allSections = [collation sectionIndexTitles];
    
    for (NSUInteger i = 0; i < [allSections count]; i++) {
        if ([mutableSections[ i ] count] > 0) {
            [existTitles addObject:allSections[ i ]];
        }
    }
    
    //create indexlist data array
    self.indexList = existTitles;
    
    return existTitleSections;
}

  • 4.TableView右侧索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return _indexList;
}
  • 5.索引小结
    索引排序关键是用UILocalizedIndexedCollation来做分组和排序,也可以自定义方法进行拼音排序,但不管什么方法,要遵循section和row的角色,将同一个字母开头的数据存储到一个section中,在section中以row的字母开头进行排序,最后保存索引列表,根据TableView右侧索引添加方法进行添加设置。

  • 6.附上传送门
    https://github.com/earthX/TableViewIndexDemo

推荐阅读更多精彩内容