美文网首页
浅析创建tabelView索引

浅析创建tabelView索引

作者: 沛2016 | 来源:发表于2017-06-26 15:22 被阅读0次

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

做出来的效果如下所示

* 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<NSString *> *)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索引

    绑定手机号的时候 会让用户选择地区 这时候就要做一个列表给用户选择 做出来的效果如下所示 代码如下

  • iOS UITableView基础

    一、UITableView的创建 UITableView *tabelView = [[UITableView a...

  • UItableView初始化时register注册cell

    最近想弄个TabelView,然后发现传统的创建cell的方法除了问题: UITableViewCell *cel...

  • MySQL--索引

    MySQL索引 查看索引 创建索引 创建唯一索引 创建主键索引 删除索引 删除主键 MySQL视图 创建视图 删除...

  • MongoDB高级应用之数据转存与恢复(5)

    1、MongoDB索引 1.1、创建索引 创建索引同时指定索引的名字 1.2、索引使用需要注意的地方 1)创建索引...

  • 2019-01-21mysql 索引

    一、索引的分类 二、索引测试 三、创建索引 1 创建表时同时创建索引 2 create在已存在的表上创建索引 3 ...

  • MongoDB 文档索引

    索引 创建索引 createIndex() & ensureIndex() 用途:创建索引语法: options ...

  • mongo相关操作

    查看是否走索引 索引常用命令 查看当前索引状态 创建普通的单列索引 创建多列索引 删除单个索引 删除所有索引 创建...

  • 五、索引

    MySQL 索引 创建索引创建表时创建索引CREATE 在已存在的表上创建索引ALTER TABLE 在已存在的表...

  • iOS开发中建立frame模型的步骤

    创建frame模型的步骤: 1.先观察界面,看是否是全屏的tabelView如果是的,就用UItableViewC...

网友评论

      本文标题:浅析创建tabelView索引

      本文链接:https://www.haomeiwen.com/subject/schjcxtx.html