美文网首页
TableView 索引 【A-Z】 电话本样式

TableView 索引 【A-Z】 电话本样式

作者: 爱喝农药de清凉 | 来源:发表于2018-04-11 11:18 被阅读27次
  • (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);
    //单单设置其中一个是无效的
    }
    }
    }

相关文章

网友评论

      本文标题:TableView 索引 【A-Z】 电话本样式

      本文链接:https://www.haomeiwen.com/subject/cosahftx.html