UITableView

1. UITableViewStyleGrouped 分区表格样式创建表格

.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;  // 雕刻,双线效果。 测试无效

2. UITableView的其他方法属性

// 表格是否进入编辑模式 ,用以显示添加,删除,移动标识

setEditing:   // 添加,删除不能同时显示

.allowsSelectionDuringEditing   // 在编辑模式下是否允许可点击选择

// 返回编辑模式下,每行左侧显示的是删除还是添加

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

//  在可处理删除/添加方法中判断每行是何种操作 在tableviewdatasource协议下

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

//  行被移动后处理方法 在tableviewdatasource协议下

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

//  自ios8开始支持的:在每行左滑或者编辑模式下点左侧删除按钮,右侧滑出多个按钮

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

3.  折叠分区内容

在分区样式下设置多个分区,每个分区对应不同的数据源

4.  UITableViewCell 定制

定制分为三种方式:纯代码,xib,storyboard

> 纯代码

-自定义类继承自UITableViewCell

-添加子视图控件属性,以使其可通过cell.方式可获取

-重写父类方法 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  // 复用时单元格初始化调用此方法,在其内实现初始化子视图,将子视图添加到cell.contentView中

/*** 开始- UITableViewCell 自适应内容高度 ***/

对于纯文字的情况(若还有其他,如图片,高度累加即可)

- 计算文字在给定宽度下所占用的尺寸

# 设定或获取文字的属性

# 计算在给定文字属性,绘制模式下,文字的高度

[string boundingRectWithSize:CGSizeMake(300, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil]

- 在返回行高协议方法中,返回计算的高度,根据实际情况可累加固定值

- 在绑定单元格内容的协议方法中,获取计算的高度,并通过此设置子视图的尺寸

/*** 结束- UITableViewCell 自适应内容高度 ***/

> xib 定制cell

- 自定类继承自UITableViewCell 并选择创建xib文件的选项

- 拖曳视图控件到xib并创建外联属性与其关联

- 选择此cell, Xcode右侧属性窗口下Table View Cell下设置 Identifier, 用于单元格复用的标识

- 若使用方式二复用,则注册时,选择registerNib:forCellReuseIdentifier:

- 其余步骤与之前基本一致

> storyboard 定制cell

UICollectionView

1. UICollectionView

网格视图,除了提供与UITableView同样的功能显示一组顺序显示的数据,还支持多列的布局。

2. UICollectionView 使用

> 设置Controller要遵循的协议:

UICollectionViewDataSource               // 数据源协议

UICollectionViewDelegate                   // 数据行为协议

UICollectionViewDelegateFlowLayout   // 数据流式布局协议

> 创建流式布局

flowLayout = [[UICollectionViewFlowLayout alloc] init]

> 设置该布局下滑动方向

flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical / Horizontal  // 默认是Vertical 在Horizontal时,元素是依次竖向摆放,Vertical时,依次横向摆放

> 以布局方式和frame 初始化UICollectionView

[[UICollectionView alloc] initWithFrame: collectionViewLayout: ]

> 设置背景色,纯代码实现时,UICollectionView默认背景色是黑色

.barckgroundColor

> 设置代理

.dataSource     // 代理 UICollectionViewDataSource

.delegate         // 代理 UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout

> 注册标识单元格UICollectionViewCellUICollectionView Cell只能使用定制化的,并且必须注册。

[collectionView registerNib: forCellWithReuseIdentifier:]

[collectionView registerClass: forCellWithReuseIdentifier:]

> 注册标识分区表头/表尾 UICollectionReusableView,创建时要手动设置其class

[collectionView registerNib: forSupplementaryViewOfKind: withReuseIdentifier: ]

/* forSupplementaryViewOfKind: 表示是为表头还是表尾注册,参数只有两个可选值:UICollectionElementKindSectionHeader:表头,UICollectionElementKindSectionFooter: 表尾  */

> 协议方法

- UICollectionViewDataSource

必须实现:

// 每个分区条数(不是行数)

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;

// 数据绑定cell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

其他方法:

// 返回分区数量

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;

// 数据绑定表头/表尾:必须为其设置尺寸才可显示,注意查看不同scrollDirection下表头的展现

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;

- UICollectionViewDelegateFlowLayout

// 返回分区表头尺寸

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;

// 返回分区表尾尺寸

- (CGSize)collectionView:(UICollectionView *)collectionView layout:

(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

// 返回每条的尺寸

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

// 返回每个分区四周的外间距

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

// 返回每个分区内最小行间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

// 返回每个分区内条目之间最小内部距离

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

-UICollectionViewDelegate

// 条被选中:注意被选中的背景色,只能自定义selectedBackgroundView,且设置后,不能设置cell的背景色,否则无法看到点击选中效果

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;

// 条被反选

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;

 

UItableView与UICollectionView的更多相关文章

  1. RumTime实践之--UITableView和UICollectionView缺省页的实现

    有关RunTime的知识点已经看过很久了,但是一直苦于在项目中没有好的机会进行实际运用,俗话说"光说不练假把式",正好最近在项目中碰到一个UITableView和UICollect ...

  2. UITableView和UICollectionView的方法学习一

    参考资料 UITableView UICollectionView UICollectionViewDataSource UICollectionViewDelegate UICollectionVi ...

  3. UItableView嵌套UICollectionView

    首先我们需要继承一下UITableView并且遵守<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UI ...

  4. UITableView和UICollectionView的Cell高度的几种设置方式

    UITableViewCell 1.UITableView的Cell高度默认由rowHeight属性指定一个低优先级的隐式约束 2.XIB中可向UITableViewCell的contentView添 ...

  5. iOS 8自动调整UITableView和UICollectionView布局

    本文转载自:http://tech.techweb.com.cn/thread-635784-1-1.html 本文讲述了UITableView.UICollectionView实现 self-siz ...

  6. iOS中UITableView和UICollectionView的默认空态页

    项目中想实现空态页风格统一控制的效果,就封装了一个默认空态页,使用的技术点有:1 方法替换 ,2 给分类(Category)添加属性. 我们知道,扩展(extension)可以给类添加私有变量和方法. ...

  7. UITableVIew与UICollectionView带动画删除cell时崩溃的处理

    UITableVIew与UICollectionView带动画删除cell时崩溃的处理 -会崩溃的原因是因为没有处理好数据源与cell之间的协调关系- 效果: tableView的源码: ModelC ...

  8. [转]iOS8 自动调整UITableView和UICollectionView布局

    转自:http://www.cocoachina.com/industry/20140825/9450.html (via:玉令天下的Blog)   本文讲述了UITableView.UICollec ...

  9. 封装Button ,封装UITableView,封装UICollectionView

    ---恢复内容开始--- 封装Button ,封装UITableView,封装UICollectionView: 1.实现Button的创建和点击事件不用分开操作处理; 2.实现UITableView ...

  10. iOS全埋点解决方案-UITableView和UICollectionView点击事件

    前言 在 $AppClick 事件采集中,还有两个比较特殊的控件: UITableView •UICollectionView 这两个控件的点击事件,一般指的是点击 UITableViewCell 和 ...

随机推荐

  1. 2、Redis入门介绍

    1.什么是Redis Redis:REmote DIctionary Server(远程字典服务器) 是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能的(key/value)分布式内存数 ...

  2. Android随笔之——Activity中启动另一应用

    最近在写语音交互程序,在语音打开应用这块碰到如何用代码控制应用启动的问题.百度了一下,有两种方案:1.获取应用的包名:2.获取应用的包名.入口类名. 之前对两种方案都进行了尝试,发现方案二中存在一个弊 ...

  3. HTML5 视频(二) <video> 使用 DOM 进行控制

    HTML5 <video> 使用 DOM 进行控制 一.HTML5 <video> 元素同样拥有方法.属性和事件. 其中的方法用于播放.暂停以及加载等.其中的属性(比如时长.音 ...

  4. 软件工程-构建之法 理解C#一小段程序

    一.前言 老师给出的要求: 阅读下面程序,请回答如下问题: 问题1:这个程序要找的是符合什么条件的数? 问题2:这样的数存在么?符合这一条件的最小的数是什么? 问题3:在电脑上运行这一程序,你估计多长 ...

  5. 关于JavaScript预编译和执行顺序以及函数引用类型的思考

    昨晚在对项目中的一部分做模块化处理的时候,遇到了一个问题,一个重新定义的function对一个通用类中的function进行赋值覆盖的时候,失败了.问题抽象出来是这样的: <script > ...

  6. C语言字符串匹配、goto语句、关机命令使用

    1.程序执行修改窗口字体颜色命令: 2.程序执行修改窗口标题命令: 3.程序执行关机倒计时命令: 4.根据提示输入团队名称JYHACK TEAM 根据提示输入团队网址:http://bbs.jyhac ...

  7. hexo在git上搭建个人博客

    公司实习第一天接到的任务是:搭建一个基于Nodejs的开源项目的开发环境,接到任务时以为不是很困难,后来才知道该项目已于去年被废弃,搭配环境的时候遇到了不少问题,折腾了两天还是没有最终完成... 不过 ...

  8. Sql Server之数据类型详解

      数据类型是一种属性,用于指定对象可保存的数据的类型,SQL Server中支持多种数据类型,包括字符类型.数值类型以及日期类型等.数据类型相当于一个容器,容器的大小决定了装的东西的多少,将数据分为 ...

  9. 用javascript编写的小游戏(getElementById , setInterval , clearInterval , window.onload , innerText 和页面跳转, 标签的使用)

    (1)图片轮转 <script type="text/javascript" > ; setInterval(function(){ var dom=document. ...

  10. Rafy 中的 Linq 查询支持(根据聚合子条件查询聚合父)

    为了提高开发者的易用性,Rafy 领域实体框架在很早开始就已经支持使用 Linq 语法来查询实体了.但是只支持了一些简单的.常用的条件查询,支持的力度很有限.特别是遇到对聚合对象的查询时,就不能再使用 ...