索引是用来辅助查询。
原则:
- 索引标题不能与显示的标题完全一样;
- 索引应该具有一定的代表性,能够代表一个数据集合;
- 如果采用了索引列表视图,一般情况下就不再使用扩展视图。(容易点到)
会重新到的数据源方法:
tableView: numberOfRowsInSection: ------获取某节的行数
tableView:cellForRowAtIndexPath: -------Cell数据的实现
numberofSectionInTableView: ----------获取节数
tableView:titleForHeaderInSection: --------节标题
sectionIndexTitlesForTableView: --------获取索引
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"team_dictionary"
ofType:@"plist"];
// 获取属性列表文件中的全部数据
self.dicData = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
NSArray *tempList = [self.dicData allKeys];
// 对key进行排序
// selector为SEL类型,sortedArrayUsingSelector方法的参数必须是selector
self.listGroupname = [tempList sortedArrayUsingSelector:@selector(compare:)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark 重写数据源方法
- (NSInteger) tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 按照节索引从小组名数组中获得组名
NSString *groupName = [self.listGroupname objectAtIndex:section];
// 将组名作为key,从字典中取出球队数组集合
// 因为存储的时候是哈希结构
NSArray *listTeams = [self.dicData objectForKey:groupName];
// 这一节一共有多少球队
return [listTeams count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// 获得选择的节 A组即第1节
NSUInteger section = [indexPath section];
// 获得选择节中选中的行索引 -- A组第1、第2.。。
NSUInteger row = [indexPath row];
// 按照节索引从小组名数组中获得组名 -- A组
NSString *groupName = [self.listGroupname objectAtIndex:section];
// 将组名作为key,从字典中取出球队数组集合
NSArray *listTeams = [self.dicData objectForKey:groupName];
cell.textLabel.text = [listTeams objectAtIndex:row];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// 该节行数
return [self.listGroupname count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// 该节的节头
NSString *groupName = [self.listGroupname objectAtIndex:section];
return groupName;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
// 添加索引
NSMutableArray *listTitles = [[NSMutableArray alloc] initWithCapacity:[self.listGroupname count]];
// 把A组改为A
for (NSString *item in self.listGroupname) {
NSString *title = [item substringToIndex:1]; // 只获取第一个字符
[listTitles addObject: title]; // 追加的字符串数组的最后一个位置
}
return listTitles;
}
@end
注意:
在故事版要配置好tableview的委托协议!!!!