直接贴代码了,很好理解,注释很全,一看就懂......

//
// ViewController.m
// TableViewSectionTitleDemo
//
// Created by 思 彭 on 2017/4/13.
// Copyright © 2017年 思 彭. All rights reserved.
// #import "ViewController.h" @interface ViewController ()<UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self setUI];
} #pragma mark - 设置界面 - (void)setUI { // 注意: 如果为group类型的tableView,会造成第一section的header的title高度有问题
/*
1.UITableViewStylePlain有多段时 段头停留(自带效果)
没有中间的间距和头部间距(要想有的重写UITableViewCell \UITableViewHeaderFooterView里面的setFrame方法)
2.UITableViewStyleGrouped
去掉头部和中间间隔
设置标头的高度为特小值 (不能为零 为零的话苹果会取默认值就无法消除头部间距了)
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)];
view.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = view;
*/ self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.tableView.tableFooterView = [[UIView alloc]init];
self.tableView.sectionIndexColor = [UIColor grayColor];
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
self.tableView.sectionHeaderHeight = 30.0f;
self.tableView.sectionFooterHeight = FLT_EPSILON; // 方法一:修改sectionHeader的背景颜色
// [[UITableViewHeaderFooterView appearance] setTintColor:[UIColor lightGrayColor]];
// 注册cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
[self.view addSubview: self.tableView];
} #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont systemFontOfSize:];
cell.textLabel.text = @"思思";
return cell;
} #pragma mark - UITableViewDelegate - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"A";
} - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView { return @[@"A", @"B", @"C", @"D", @"E"];
} - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { return ;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } // 6.0以上使用
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// 方法二:修改sectionHeader的背景颜色 Background color
// view.tintColor = [UIColor blackColor]; // Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
header.backgroundColor = [UIColor redColor]; // 方法三:修改sectionHeader的背景颜色
header.contentView.backgroundColor = [UIColor lightGrayColor];
// Another way to set the background color
// Note: does not preserve gradient effect of original header
// header.contentView.backgroundColor = [UIColor blackColor];
} /* 方法四:修改sectionHeader的背景颜色
-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
UIView* content = castView.contentView;
UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
content.backgroundColor = color;
}
}
*/ // 让段头不停留(取消粘性效果)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat sectionHeaderHeight = ;
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, , , );
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) { scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, , , );
}
} @end

最新文章

  1. HTML5_06之拖放API、Worker线程、Storage存储
  2. DLUTOJ1216
  3. iPad 2升级iOS 9的过程记录
  4. Remove Duplicates from Sorted List I &amp; II
  5. winPcap_4_获取已安装设备的高级信息
  6. Nmon 性能:分析 AIX 和 Linux 性能的免费工具
  7. js 调用父窗口的方法
  8. 设计模式-GoF23
  9. sscanf( )函数初体验
  10. js面向对象学习笔记(五):tab切换
  11. [Noi2015]荷马史诗
  12. iframe中 父页面和子页面查找元素的方法
  13. 【原创】那些年用过的Redis集群架构(含面试解析)
  14. MySQL 数据查询
  15. 剑指offer面试题27:二叉搜索树与双向链表
  16. in exists
  17. 按字典序依次打印只由1~n组成的n位数
  18. 兼容多种模块规范(AMD,CMD,Node)的代码
  19. C#语言————第三章 使用属性升级MyBank
  20. django DateTimeField 时间格式化

热门文章

  1. BZOJ 最大公约数 (通俗易懂&amp;效率高&amp;欧拉函数)
  2. Pursuit For Artifacts CodeForces - 652E (Tarjan+dfs)
  3. mybatis总体分析
  4. IO框架:asyncio 上篇
  5. linux内核 进程管理
  6. 搭建私有CA
  7. 生产问题之StackOverflowError异常小记
  8. mysql router使用配置
  9. CentOS 安装oracle client
  10. 【leetcode】1275. Find Winner on a Tic Tac Toe Game