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

  1. //
  2. // ViewController.m
  3. // TableViewSectionTitleDemo
  4. //
  5. // Created by 思 彭 on 2017/4/13.
  6. // Copyright © 2017年 思 彭. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
  12.  
  13. @property (nonatomic, strong) UITableView *tableView;
  14.  
  15. @end
  16.  
  17. @implementation ViewController
  18.  
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self setUI];
  22. }
  23.  
  24. #pragma mark - 设置界面
  25.  
  26. - (void)setUI {
  27.  
  28. // 注意: 如果为group类型的tableView,会造成第一section的header的title高度有问题
  29. /*
  30. 1.UITableViewStylePlain有多段时 段头停留(自带效果)
  31. 没有中间的间距和头部间距(要想有的重写UITableViewCell \UITableViewHeaderFooterView里面的setFrame方法)
  32. 2.UITableViewStyleGrouped
  33. 去掉头部和中间间隔
  34. 设置标头的高度为特小值 (不能为零 为零的话苹果会取默认值就无法消除头部间距了)
  35. UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)];
  36. view.backgroundColor = [UIColor redColor];
  37. self.tableView.tableHeaderView = view;
  38. */
  39.  
  40. self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
  41. self.tableView.delegate = self;
  42. self.tableView.dataSource = self;
  43. self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
  44. self.tableView.tableFooterView = [[UIView alloc]init];
  45. self.tableView.sectionIndexColor = [UIColor grayColor];
  46. self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
  47. self.tableView.sectionHeaderHeight = 30.0f;
  48. self.tableView.sectionFooterHeight = FLT_EPSILON;
  49.  
  50. // 方法一:修改sectionHeader的背景颜色
  51. // [[UITableViewHeaderFooterView appearance] setTintColor:[UIColor lightGrayColor]];
  52. // 注册cell
  53. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
  54. [self.view addSubview: self.tableView];
  55. }
  56.  
  57. #pragma mark - UITableViewDataSource
  58.  
  59. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  60.  
  61. return ;
  62. }
  63.  
  64. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  65.  
  66. return ;
  67. }
  68.  
  69. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  70.  
  71. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  72. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  73. cell.textLabel.font = [UIFont systemFontOfSize:];
  74. cell.textLabel.text = @"思思";
  75. return cell;
  76. }
  77.  
  78. #pragma mark - UITableViewDelegate
  79.  
  80. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  81.  
  82. return @"A";
  83. }
  84.  
  85. - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  86.  
  87. return @[@"A", @"B", @"C", @"D", @"E"];
  88. }
  89.  
  90. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
  91.  
  92. return ;
  93. }
  94.  
  95. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  96.  
  97. }
  98.  
  99. // 6.0以上使用
  100. -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
  101. {
  102. // 方法二:修改sectionHeader的背景颜色 Background color
  103. // view.tintColor = [UIColor blackColor];
  104.  
  105. // Text Color
  106. UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
  107. [header.textLabel setTextColor:[UIColor whiteColor]];
  108. header.backgroundColor = [UIColor redColor];
  109.  
  110. // 方法三:修改sectionHeader的背景颜色
  111. header.contentView.backgroundColor = [UIColor lightGrayColor];
  112. // Another way to set the background color
  113. // Note: does not preserve gradient effect of original header
  114. // header.contentView.backgroundColor = [UIColor blackColor];
  115. }
  116.  
  117. /* 方法四:修改sectionHeader的背景颜色
  118. -(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
  119. if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
  120. UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
  121. UIView* content = castView.contentView;
  122. UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
  123. content.backgroundColor = color;
  124. }
  125. }
  126. */
  127.  
  128. // 让段头不停留(取消粘性效果)
  129. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  130.  
  131. CGFloat sectionHeaderHeight = ;
  132. if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=) {
  133. scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, , , );
  134. } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
  135.  
  136. scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, , , );
  137. }
  138. }
  139.  
  140. @end

UITableView的使用总结的更多相关文章

  1. iOS UITableView 与 UITableViewController

    很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名.项目地址. UITab ...

  2. UITableView(二)

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  3. iOS: 在UIViewController 中添加Static UITableView

    如果你直接在 UIViewController 中加入一个 UITableView 并将其 Content 属性设置为 Static Cells,此时 Xcode 会报错: Static table ...

  4. iOS 编辑UITableView(根据iOS编程编写)

    上个项目我们完成了 JXHomepwner 简单的应用展示,项目地址.本节我们需要在上节项目基础上,增加一些响应用户操作.包括添加,删除和移动表格. 编辑模式 UITableView 有一个名为  e ...

  5. 使用Autolayout实现UITableView的Cell动态布局和高度动态改变

    本文翻译自:stackoverflow 有人在stackoverflow上问了一个问题: 1 如何在UITableViewCell中使用Autolayout来实现Cell的内容和子视图自动计算行高,并 ...

  6. iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法

    "UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...

  7. UITableView cell复用出错问题 页面滑动卡顿问题 & 各杂七杂八问题

    UITableView 的cell 复用机制节省了内存,但是有时对于多变的自定义cell,重用时会出现界面出错(例如复用出错,出现cell混乱重影).滑动卡顿等问题,这里只简单敲下几点复用出错时的解决 ...

  8. UITableview delegate dataSource调用探究

    UITableview是大家常用的UIKit组件之一,使用中我们最常遇到的就是对delegate和dataSource这两个委托的使用.我们大多数人可能知道当reloadData这个方法被调用时,de ...

  9. UITableView点击每个Cell,Cell的子内容的收放

    关于点击TableviewCell的子内容收放问题,拿到它的第一个思路就是, 方法一: 运用UITableview本身的代理来处理相应的展开收起: 1.代理:- (void)tableView:(UI ...

  10. 使用UITableView的分组样式

    分组样式顾名思义是对TableView中的数据行进行分组处理,每个分组都有一个header和footer. TableView中header的英文文本是大写的,footer的英文文本是小写的.如下图浅 ...

随机推荐

  1. QTP(7)

    一.输出值(Output Value) 1.应用场景: 1) 关心被测系统的数据 2) 将被测系统生成的数据作为后面步骤的输入 2.输出值就是输出被测系统中实际运行时的数据的一种技术 a.运行中对象的 ...

  2. CSS基础学习-12.CSS position

    绝对定位 position:absolute,元素脱离文档流,然后使用left.right.top.bottom属性相对于其最接近的一个具有定位属性的祖先元素进行绝对定位.如果不存在这样的祖先元素,则 ...

  3. 喜大普奔!.NET界面控件DevExpress v19.2发布,快来下载体验

    DevExpress Universal Subscription(又名DevExpress宇宙版或DXperience Universal Suite)是全球使用广泛的.NET用户界面控件套包,De ...

  4. Java-DatabaseConnectionPool工具类

    package org.zxjava.test; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.s ...

  5. 对webview的研究--------引用

    简要的说,webview能够在移动应用中开辟出一个窗口,在里面显示html页面,css以及js代码也可以被解析执行,它使用的是我们熟悉的webkit内核.android和ios都有相应的API,所以写 ...

  6. css百分比值到底参考谁?

    一.元素宽高设置百分比 (1)width / min-width / max-width 参考块级父元素的宽度 (2)height / min-height / max-height 参考块级父元素的 ...

  7. jquery reset选择器 语法

    jquery reset选择器 语法 作用::reset 选择器选取类型为 reset 的 <button> 和 <input> 元素.直线电机滑台 语法:$(":r ...

  8. XML -- 为什么选择XML?

    1.XML是什么,主要功能? XML全称(EXtensible Markup Language),是可扩展性标记语言. XML主要功能是用来传输和存储数据.它就是一种纯文本.只要程序能访问纯文本就能访 ...

  9. codeforces271D

    Good Substrings CodeForces - 271D 给你一个只包含小写字母的字符串s.问你在这个字符串中有多少个不同的子串.且要求这些子串中不得出现超过k个的特殊字母.*子串s1和子串 ...

  10. BZOJ 4814 Luogu P3699 [CQOI2017]小Q的草稿 (计算几何、扫描线、set)

    题目链接 (BZOJ) http://lydsy.com/JudgeOnline/problem.php?id=4814 (Luogu) https://www.luogu.org/problem/P ...