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
表格索引的高級設置
上面舉得是最簡單的小例子,已經將要顯示的數據進行了排序,實際上真正在開發的過程中,不需要手動排序,可以使用第三方的方法,將所有的城市轉為拼音,按照首字母進行分組排序,然后索引也根據拼音首字母自動獲得。
疑問咨詢或技術交流,請加入官方QQ群: (452379712)
新聞熱點
疑難解答