iOS开发 实现字母索引最快捷方法

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray                 *_dataArray;             //数据源
    UITableView          *_tableView;
    NSMutableArray   *_indexArray;           //存下标数组
    NSMutableArray   *_sectionArray;        //存处理过以后的数组
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = [NSArray arrayWithObjects:@"控件", @"感觉",@"iu",@"你吧",@"温柔",@"啊啊",@"骗局",@"门口",@"呃呃",@"快播",@"温上上",@"急哦",@"mm",@"ush",@"牛逼",@"2千万",@"&gj",@"#9",@"*gb",@"228",nil];
    _indexArray = [NSMutableArray array];
    _sectionArray = [NSMutableArray array];
    
    [self addTableView];
    [self ProcessData];  //整理数据
}

#pragma mark - 列表
- (void)addTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64,  SCREEN_WIDTH, SCREEN_HEIGHT-64) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.tableHeaderView = [[UIView alloc] init];
    _tableView.separatorInset = UIEdgeInsetsMake(0, 64, SCREEN_WIDTH-64, 0.5);
    [self.view addSubview:_tableView];
}
-(void)ProcessData{
    for (int i = 0; i < _dataArray.count; i ++) {
        NSString *str = _dataArray[i]; //一开始的内容
        if (str.length) {  //下面那2个转换的方法一个都不能少
            NSMutableString *ms = [[NSMutableString alloc] initWithString:str];
            //汉字转拼音
            if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {
            }
            //拼音转英文
            if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
                //字符串截取第一位
                NSString *firstStr = [ms substringToIndex:1];
                //如果不是字母开头的,转为#
                BOOL isLetter = [self MatchLetter:firstStr];
                 if (!isLetter){
                    firstStr = @"#";
                }else{
                    //小写字母转大写
                    firstStr = [firstStr uppercaseString];
                }
                
                //如果还没有索引
                if (_indexArray.count <= 0) {
                    //保存当前这个做索引
                    [_indexArray addObject:firstStr];
                    //用这个字母做字典的key,将当前的标题保存到key对应的数组里面去
                    NSMutableArray *array = [NSMutableArray arrayWithObject:str];
                    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:array,firstStr, nil];
                    [_sectionArray addObject:dic];
                }else{
                    //如果索引里面包含了当前这个字母,直接保存数据
                    if ([_indexArray containsObject:firstStr]) {
                        //取索引对应的数组,保存当前标题到数组里面
                        NSMutableArray *array = _sectionArray[0][firstStr];
                        [array addObject:str];
                        //重新保存数据
                        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:array,firstStr, nil];
                        [_sectionArray addObject:dic];
                    }else{
                        //如果没有包含,说明是新的索引
                        [_indexArray addObject:firstStr];
                        //用这个字母做字典的key,将当前的标题保存到key对应的数组里面去
                        NSMutableArray *array = [NSMutableArray arrayWithObject:str];
                        NSMutableDictionary *dic = _sectionArray[0];
                        [dic setObject:array forKey:firstStr];
                        [_sectionArray addObject:dic];
                    }
                }
            }
        }
    }
    
    //将字母排序
    NSArray *compareArray = [[_sectionArray[0] allKeys] sortedArrayUsingSelector:@selector(compare:)];
    _indexArray = [NSMutableArray arrayWithArray:compareArray];
    
    //判断第一个是不是字母,如果不是放到最后一个
    BOOL isLetter = [self MatchLetter:_indexArray[0]];
    if (!isLetter) {
        //获取数组的第一个元素
        NSString *firstStr = [_indexArray firstObject];
        //移除第一项元素
        [_indexArray removeObjectAtIndex:0];
        //插入到最后一个位置
        [_indexArray insertObject:firstStr atIndex:_indexArray.count];
    }
    
    [_tableView reloadData];
}

#pragma mark - tableView代理
//列表分为几组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _indexArray.count;
}

//每一组分多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_sectionArray[0][_indexArray[section]] count];
}

//每一个cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

//每一个分组的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

//每一个分组里面显示的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    headerView.backgroundColor = [UIColor grayColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 20)];
    label.text = _indexArray[section];
    label.textColor = [UIColor redColor];
    label.font = [UIFont boldSystemFontOfSize:15];
    label.backgroundColor = [UIColor clearColor];
    [headerView addSubview:label];

    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell_identifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    
    while (cell.contentView.subviews.lastObject) {
        [cell.contentView.subviews.lastObject removeFromSuperview];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSArray *currentArray = _sectionArray[0][_indexArray[indexPath.section]];
    cell.textLabel.text =currentArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

//设置右侧索引的标题,这里返回的是一个数组哦!
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return _indexArray;
}

#pragma mark - 匹配是不是字母开头
-(BOOL)MatchLetter:(NSString *)str
{
    //判断是否以字母开头
    NSString *ZIMU = @"^[A-Za-z]+$";
    NSPredicate *regextestA = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", ZIMU];
    
    if ([regextestA evaluateWithObject:str] == YES)
        return YES;
    else
        return NO;
}

 

转载于:https://my.oschina.net/linxiaoxi1993/blog/602220

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值