IOS UITableView索引排序功能

来源:互联网 发布:三国群英传8武将数据 编辑:程序博客网 时间:2024/04/30 15:34

UITbableView分组展示信息时,有时在右侧会带索引,右侧的索引一般为分组的首字母,比如城市列表的展示。当点击右侧索引的字母,列表会快速跳到索引对应的分组,方便我们快速查找。下面,就介绍一下索引的最简单地设置。


设置表格索引的步骤:

    1、添加表格,设置代理和数据源    2、得到要显示的数据的数组    3、得到右侧显示索引的数组,索引数组中元素的个数要与显示的分组数量对应    4、实现tableview中必须实现的几个方法    5、实现sectionIndexTitlesForTableView:方法,在这个方法中返回索引数组    6、实现titleForHeaderInSection:方法,设置组标题,一般右侧索引与组标题内容是一致的

代码:

    //  ViewController.m      //  TableView索引      //      //  Created by jerehedu on 15/6/11.      //  Copyright (c) 2015年 jerehedu. All rights reserved.      //            #import "ViewController.h"            @interface ViewController ()<UITableViewDataSource, UITableViewDelegate>      {          UITableView *_tableView;                    NSArray *_citysAry; //要显示的城市数组                    NSMutableArray *_indexArray; //索引数组      }            @end            @implementation ViewController            - (void)viewDidLoad {          [super viewDidLoad];                   _citysAry = @[                       @[@"北京",@"上海",@"广州",@"烟台"],                       @[@"阿坝",@"阿克苏",@"安康",@"安阳",@"澳门"],                       @[@"北京",@"白城",@"白山",@"包头",@"宝鸡",@"保定",@"滨州"],                       @[@"重庆",@"成都",@"长沙",@"长春",@"沧州",@"赤峰"],                       @[@"大连",@"东莞",@"达州",@"大理",@"大同",@"大兴安岭",@"丹东",@"东营"],                       @[@"鄂尔多斯",@"鄂州",@"恩施"],                       @[@"佛山",@"福州",@"抚顺",@"阜新",@"阜阳"],                       @[@"广州",@"贵阳",@"桂林",@"甘南",@"格尔木",@"广安",@"广元"],                       @[@"杭州",@"海口",@"哈尔滨",@"合肥",@"哈密",@"海北",@"海东",@"海西",@"海南",@"邯郸",@"菏泽",@"鹤岗",@"黄山"],                       @[@"济南",@"锦州",@"吉林",@"济宁",@"嘉兴",@"嘉峪关",@"金华",@"荆门",@"荆州",@"酒泉",@"景德镇"],                       @[@"昆明",@"喀什",@"开封",@"克拉玛依"],                       @[@"洛阳",@"拉萨",@"兰州",@"莱芜",@"廊坊",@"乐山",@"丽江",@"连云港",@"辽阳",@"临汾",@"临沂",@"六盘水"],                       @[@"茂名",@"马鞍山",@"牡丹江"],                       @[@"南京",@"宁波",@"南昌",@"南宁",@"南通",@"宁德"],                       @[@"攀枝花",@"盘锦",@"平顶山",@"普洱"],                       @[@"青岛",@"齐齐哈尔",@"黔南",@"秦皇岛",@"庆阳",@"琼海",@"泉州"],                       @[@"日喀什",@"日照"],                       @[@"上海",@"深圳",@"沈阳",@"石家庄",@"苏州",@"三门峡",@"三亚",@"汕头",@"绍兴",@"十堰",@"石河子",@"松原"],                       @[@"天津",@"唐山",@"台湾",@"太原",@"泰州",@"泰安",@"通辽",@"吐鲁番"],                       @[@"武汉",@"无锡",@"潍坊",@"乌鲁木齐",@"威海",@"五指山"],                       @[@"西安",@"厦门",@"徐州",@"西沙",@"仙桃",@"咸阳",@"孝感"],                       @[@"银川",@"雅安",@"烟台",@"延安",@"盐城",@"扬州",@"阳江",@"宜宾",@"宜昌",@"玉树"],                       @[@"郑州",@"珠海",@"淄博",@"漳州",@"张家口",@"驻马店",@"遵义"],                       ];                    //添加tableview          _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20) style:UITableViewStylePlain];          _tableView.dataSource = self;          _tableView.delegate = self;          [self.view addSubview:_tableView];                    //索引数组          _indexArray = [NSMutableArray arrayWithObjects:@"#",nil];          for (char ch='A'; ch<='Z'; ch++) {              if (ch=='I' || ch=='O' || ch=='U' || ch=='V')                  continue;              [_indexArray addObject:[NSString stringWithFormat:@"%c",ch]];          }      }            #pragma mark tableview 每组行数      -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section      {          return [[_citysAry objectAtIndex:section] count];      }            #pragma mark 组数      -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView      {          return _indexArray.count;      }            #pragma mark 设置每行内容      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath      {          static NSString *idStr = @"cell";          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idStr];          if (cell == nil) {              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idStr];          }                    cell.textLabel.text = _citysAry[indexPath.section][indexPath.row];                    return cell;      }            #pragma mark 设置组标题      - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section      {          return [_indexArray objectAtIndex:section];      }            #pragma mark 右侧索引      -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView      {          return _indexArray;      }            @end  

表格索引的高级设置

上面举得是最简单的小例子,已经将要显示的数据进行了排序,实际上真正在开发的过程中,不需要手动排序,可以使用第三方的方法,将所有的城市转为拼音,按照首字母进行分组排序,然后索引也根据拼音首字母自动获得。



0 0