浅析创建tabelView索引

绑定手机号的时候 会让用户选择地区 这时候就要做一个列表给用户选择

做出来的效果如下所示

*
浅析创建tabelView索引_第1张图片
A8国家列表_PxCook.png

代码如下

- (void)viewDidLoad {
    
[super viewDidLoad];
    
  [self initData];
    
  [self.view addSubview:self.tableView];
    
    [self ProcessData];  //整理数据
    
 }

- (void)initData {
    
    _dataArray = [NSArray arrayWithObjects:@"中国",@"香港",@"台湾",@"澳门",@"控件",@"笔",@"成",@"感觉",@"iu",@"你吧",@"温柔",@"啊啊",@"骗局",@"门口",@"呃呃",@"快播",@"温上上",@"急哦",@"mm",@"ush",@"牛逼",nil];
    
    _indexArray   = [NSMutableArray array];
    _sectionArray = [NSMutableArray array];
    
}


 -(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];
                
                //转化为大写的
               firstStr = [firstStr uppercaseString];
                
               //转为*
                if ([str isEqualToString:@"中国"] ||
                    [str isEqualToString:@"香港"] ||
                    [str isEqualToString:@"澳门"] ||
                    [str isEqualToString:@"台湾"])
                {
                    firstStr = @"* ";
                }
                
                
               //如果还没有索引
                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];
    
    [_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 56;
 }

 //每一个分组的高度
 - (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 whiteColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(22, 0, 200, 20)];
    
    label.text = (0 == section) ? @"常用" : _indexArray[section];
    
    label.font = [UIFont boldSystemFontOfSize:12];
    
    label.backgroundColor = [UIColor clearColor];
    
    [headerView addSubview:label];
    
    //分割线
    UIView * lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 19.5, SCREEN_WIDTH, 0.5)];
    
    lineV.backgroundColor = [UIColor darkGrayColor];
    
    [headerView addSubview:lineV];
    
    return headerView;
 }

> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JPTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"JPTableViewCell"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSArray *currentArray = _sectionArray[0][_indexArray[indexPath.section]];
    
    cell.countryLabel.text  = currentArray[indexPath.row];
    
    if (indexPath.row == [_sectionArray[0][_indexArray[indexPath.section]] count] -1) {
        cell.lineView.frame = CGRectMake(0, 55.5, 600, 0.5);
    }else{
        cell.lineView.frame = CGRectMake(45, 55.5, 600, 0.5);
    }
    
    return cell;
 }

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击了第%zd组,第%zd个",indexPath.section,indexPath.row);
    NSLog(@"选择的国家是 == %@",_sectionArray[0][_indexArray[indexPath.section]][indexPath.row]);
 }

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

 #pragma mark ============ 访问器 ============
 - (UITableView *)tableView {
    if (_tableView == nil) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64,  SCREEN_WIDTH, SCREEN_HEIGHT-64) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //索引的字体颜色
        _tableView.sectionIndexColor = [UIColor blackColor];
        
        _tableView.sectionIndexBackgroundColor = [UIColor clearColor];
        
        //取消分割线
        _tableView.separatorStyle = NO;
        
        [_tableView registerClass:[JPTableViewCell class] forCellReuseIdentifier:@"JPTableViewCell"];
    }
    return _tableView;
 }

你可能感兴趣的:(浅析创建tabelView索引)