1、自定义索引
准备数据,设计一个数组,数组里面的元素为每个section的数组。
//返回Section总数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.indextitles.count;
}
//返回每个Section的行数
-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section
{
return [[self.sectionarray objectAtIndex:section] count];
}
//返回每个Section的title
-(NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section
{
return [self.indextitles objectAtIndex:section];
}
//返回索引栏数据
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.indextitles;
}
//建立索引栏和section的关联
-(NSInteger)tableView:(UITableView *)tableViewsectionForSectionIndexTitle:(NSString*)titleatIndex:(NSInteger)index
{
return [self.indextitles indexOfObject:title];
}
2、使用UILocalizedIndexedCollation建立
创建一个UILocalizedIndexedCollation 这个其实固定为27个字符 A-Z 加 # 换句话说,就是索引栏固定为A-Z加
#,创建的表格一定有27个Section,如果没有数据的话会有titleForHeaderInSection
//返回Section总数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.collation.sectionTitles count];
}
//返回每个Section的title
-(NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section
{
return [self.collation.sectionIndexTitles objectAtIndex:section];
}
//返回索引栏的列别数据
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.collation.sectionTitles;
}
//建立关联
-(NSInteger)tableView:(UITableView *)tableViewsectionForSectionIndexTitle:(NSString*)titleatIndex:(NSInteger)index
{
return [self.collation sectionForSectionIndexTitleAtIndex:index];
}
//每个Section的行数
-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section
{
NSArray *innerArray= [self.outerArray objectAtIndex:section];
return [innerArray count];
}