UITableView的使用总结
直接贴代码了,很好理解,注释很全,一看就懂......
- //
- // 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
UITableView的使用总结的更多相关文章
- iOS UITableView 与 UITableViewController
很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名.项目地址. UITab ...
- UITableView(二)
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- iOS: 在UIViewController 中添加Static UITableView
如果你直接在 UIViewController 中加入一个 UITableView 并将其 Content 属性设置为 Static Cells,此时 Xcode 会报错: Static table ...
- iOS 编辑UITableView(根据iOS编程编写)
上个项目我们完成了 JXHomepwner 简单的应用展示,项目地址.本节我们需要在上节项目基础上,增加一些响应用户操作.包括添加,删除和移动表格. 编辑模式 UITableView 有一个名为 e ...
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
本文翻译自:stackoverflow 有人在stackoverflow上问了一个问题: 1 如何在UITableViewCell中使用Autolayout来实现Cell的内容和子视图自动计算行高,并 ...
- iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法
"UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...
- UITableView cell复用出错问题 页面滑动卡顿问题 & 各杂七杂八问题
UITableView 的cell 复用机制节省了内存,但是有时对于多变的自定义cell,重用时会出现界面出错(例如复用出错,出现cell混乱重影).滑动卡顿等问题,这里只简单敲下几点复用出错时的解决 ...
- UITableview delegate dataSource调用探究
UITableview是大家常用的UIKit组件之一,使用中我们最常遇到的就是对delegate和dataSource这两个委托的使用.我们大多数人可能知道当reloadData这个方法被调用时,de ...
- UITableView点击每个Cell,Cell的子内容的收放
关于点击TableviewCell的子内容收放问题,拿到它的第一个思路就是, 方法一: 运用UITableview本身的代理来处理相应的展开收起: 1.代理:- (void)tableView:(UI ...
- 使用UITableView的分组样式
分组样式顾名思义是对TableView中的数据行进行分组处理,每个分组都有一个header和footer. TableView中header的英文文本是大写的,footer的英文文本是小写的.如下图浅 ...
随机推荐
- QTP(7)
一.输出值(Output Value) 1.应用场景: 1) 关心被测系统的数据 2) 将被测系统生成的数据作为后面步骤的输入 2.输出值就是输出被测系统中实际运行时的数据的一种技术 a.运行中对象的 ...
- CSS基础学习-12.CSS position
绝对定位 position:absolute,元素脱离文档流,然后使用left.right.top.bottom属性相对于其最接近的一个具有定位属性的祖先元素进行绝对定位.如果不存在这样的祖先元素,则 ...
- 喜大普奔!.NET界面控件DevExpress v19.2发布,快来下载体验
DevExpress Universal Subscription(又名DevExpress宇宙版或DXperience Universal Suite)是全球使用广泛的.NET用户界面控件套包,De ...
- Java-DatabaseConnectionPool工具类
package org.zxjava.test; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.s ...
- 对webview的研究--------引用
简要的说,webview能够在移动应用中开辟出一个窗口,在里面显示html页面,css以及js代码也可以被解析执行,它使用的是我们熟悉的webkit内核.android和ios都有相应的API,所以写 ...
- css百分比值到底参考谁?
一.元素宽高设置百分比 (1)width / min-width / max-width 参考块级父元素的宽度 (2)height / min-height / max-height 参考块级父元素的 ...
- jquery reset选择器 语法
jquery reset选择器 语法 作用::reset 选择器选取类型为 reset 的 <button> 和 <input> 元素.直线电机滑台 语法:$(":r ...
- XML -- 为什么选择XML?
1.XML是什么,主要功能? XML全称(EXtensible Markup Language),是可扩展性标记语言. XML主要功能是用来传输和存储数据.它就是一种纯文本.只要程序能访问纯文本就能访 ...
- codeforces271D
Good Substrings CodeForces - 271D 给你一个只包含小写字母的字符串s.问你在这个字符串中有多少个不同的子串.且要求这些子串中不得出现超过k个的特殊字母.*子串s1和子串 ...
- BZOJ 4814 Luogu P3699 [CQOI2017]小Q的草稿 (计算几何、扫描线、set)
题目链接 (BZOJ) http://lydsy.com/JudgeOnline/problem.php?id=4814 (Luogu) https://www.luogu.org/problem/P ...