-
(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.charArr;
} -
(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
NSInteger count = 0;
for (NSString * charStr in self.charArr) {
if ([charStr isEqualToString:title] ) { return count; }else{ count ++; }
}
return count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.charArr[section];
}
-
(NSMutableArray *)charArr{
if (!_charArr) {_charArr = [[NSMutableArray alloc] init]; for (char c = 'A'; c <= 'Z'; c++) { [_charArr addObject:[NSString stringWithFormat:@"%c",c]]; }
}
return _charArr;
}
修改索引 颜色
tableView.sectionIndexBackgroundColor = [UIColor greenColor];//修改右边索引的背景色
tableView.sectionIndexColor = [UIColor orangeColor];//修改右边索引字体的颜色
tableView.sectionIndexTrackingBackgroundColor = [UIColor orangeColor];//修改右边索引点击时候的背景色
修改索引字体大小
-
(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 fontWithName:@"AmericanTypewriter" size:11] forKey:@"_font"];
//设置view的大小
view.bounds = CGRectMake(0, 0, 30, tableView.height);
//单单设置其中一个是无效的
}
}
}
网友评论