其实tableView的索引的实现非常简单,主要是靠下面这个方法再结合多分区实现的:
//建立浮动索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:@"S",@"H",@"S", nil];
}
以下的代码中的data是一个字典,indexs是字典的key所组成的数组。用字典中的数组来填充多分区中的数据。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [indexs count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *secNum = [indexs objectAtIndex:section];
return [[data objectForKey:secNum] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NSInteger rowNo = indexPath.row;
NSInteger rowSe = indexPath.section;
cell.textLabel.text = [[data objectForKey:[indexs objectAtIndex:rowSe]] objectAtIndex:rowNo];
return cell;
}
//建立浮动索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:@"S",@"H",@"S", nil];
}
//分区页眉
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [indexs objectAtIndex:section];
}
//分区页脚
//-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
// NSString *str = [NSString stringWithFormat:@"一共有%lu个人物",(unsigned long)[[data objectForKey:[indexs objectAtIndex:section]] count]];
// return str;
//}