一索引相关属性
//索引相关属性
//设置默认时索引字符的的颜色
//tableView.sectionIndexColor = [UIColor redColor];
//选中时索引条的背景色
// tableView.sectionIndexTrackingBackgroundColor = [UIColor lightGrayColor];
//设置默认时,索引条的背景颜色
// tableView.sectionIndexBackgroundColor = [UIColor yellowColor];
二.索引相关代理方法
/多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return array.count;
}
//每组多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
//cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
cell.textLabel.text = [NSString stringWithFormat:@"我是第%@组第%ld行",array[indexPath.section],indexPath.row];
return cell;
}
//组标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return array[section];
}
//返回一个索引数组
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return array;
}
//点击索引时的委托方法
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
NSLog(@"点击了标题是%@,点击了index---%ld",title,index);
lable.isAlpha = YES;
lable.text = [NSString stringWithFormat:@"%@",title];
return index;
}
//设置索引字体大小
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
for (UIView *view in [tableView subviews]) {
if ([view isKindOfClass:[NSClassFromString(@"UITableViewIndex") class]]) {
// 设置字体大小
[view setValue:[UIFont systemFontOfSize:20]forKey:@"_font"];
//设置view的大小
view.bounds = CGRectMake(0, 0, 30,30);
//单单设置其中一个是无效的
}
}
}