UITableView展示的数据如果非常多,我们通常需要根据各个分区产生一个索引快速定位到需要的分区。本例子需要创建一个plist,内容如下:
teams.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>A</key>
<array>
<string>A1-南非</string>
<string>A2-墨西哥</string>
<string>A3-乌拉圭</string>
<string>A4-法国</string>
</array>
<key>B</key>
<array>
<string>B1-阿根廷</string>
<string>B2-尼日利亚</string>
<string>B3-韩国</string>
<string>B4-希腊</string>
</array>
<key>C</key>
<array>
<string>C1-英格兰</string>
<string>C2-美国</string>
<string>C3-阿尔及利亚</string>
<string>C4-斯洛文尼亚</string>
</array>
<key>D</key>
<array>
<string>D1-德国</string>
<string>D2-澳大利亚</string>
<string>D3-塞尔维亚</string>
<string>D4-加纳</string>
</array>
<key>E</key>
<array>
<string>E1-荷兰</string>
<string>E2-丹麦</string>
<string>E3-日本</string>
<string>E4-喀麦隆</string>
</array>
<key>F</key>
<array>
<string>F1-意大利</string>
<string>F2-巴拉圭</string>
<string>F3-斯洛伐克</string>
<string>F4-新西兰</string>
</array>
<key>G</key>
<array>
<string>G1-巴西</string>
<string>G2-朝鲜</string>
<string>G3-科特迪瓦</string>
<string>G4-葡萄牙</string>
</array>
<key>H</key>
<array>
<string>H1-西班牙</string>
<string>H2-瑞士</string>
<string>H3-洪都拉斯</string>
<string>H4-智利</string>
</array>
</dict>
</plist>
ViewController.m
//
// ViewController.m
// UITableViewIndexDemo
//
// Created by Apple on 16/5/25.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
// 从plist文件中读取的数据
NSDictionary * dictData;
// 分区名字
NSArray * listGroupname;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"teams"
ofType:@"plist"];
// 获取属性列表文件中的全部数据
dictData = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
NSArray *tempList = [dictData allKeys];
// 对key进行排序
listGroupname = [tempList sortedArrayUsingSelector:@selector(compare:)];
UITableView* tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
//设置tableView的数据源
tableView.dataSource = self;
//设置tableView的代理
tableView.delegate = self;
//设置索引背景颜色
tableView.sectionIndexBackgroundColor = [UIColor clearColor];
[self.view addSubview:tableView];
}
#pragma mark - UITableViewDataSource
// 返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [listGroupname count];
}
// 返回一个分区中有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 1. 先获取分区的名字
NSString *groupName = [listGroupname objectAtIndex:section];
// 2. 然后根据分区名字,从字典中获取每个分区的元素数量
NSArray *listTeams = [dictData objectForKey:groupName];
return [listTeams count];
}
// 设置组头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// 按照节索引从小组名数组中获取组名
NSString *grounpName = [listGroupname objectAtIndex:section];
return grounpName;
}
// 返回cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//获得选择的节
NSInteger section = [indexPath section];
//获得选择节中的,选中的行索引
NSUInteger row = [indexPath row];
//按照节索引从小组名数组中获得组名
NSString *groupName = [listGroupname objectAtIndex:section];
//按照组名作为key,从字典中取出球队数组集合
NSArray *listTeams = [dictData objectForKey:groupName];
//按照行索引从球队组合中取出球队名字
cell.textLabel.text = [listTeams objectAtIndex:row];
return cell;
}
// 设置索引名字,A组设置所以为A
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
// 创建一个可变数组当做索引数组,数组大小是分区名字数组大小
NSMutableArray *listTitles = [[NSMutableArray alloc] initWithCapacity:[listGroupname count]];
// 循环分区名字数组,把 A组 -> A
for (NSString *item in listGroupname) {
// 获得第一个字符即A[B]
NSString *title = [item substringToIndex:1];
// 将第一个字符添加到索引数组
[listTitles addObject:title];
}
// 返回索引数组
return listTitles;
}
@end
效果图如下:
下图为点击了E而快速跳到的分区效果