** 序言:**
我一直以来就想写一个 UITableView 的专题,但是一直苦于没有时间。
当时间来的时候,发现自己沉溺于各种杂乱的事情中。
其实,要做一件事情是没有那么多的理由。理由只是用来欺骗自己的。做事,要的就是马上,立即,现在,再加上持久的坚持!
go!
1、UITableView 基本介绍
在 iOS 开发中 UITableView 是我们使用最广的控件。UITableView继承自UIScrollView,因此支持垂直滚动,⽽且性能极佳 。在我们的手机 app 中到处可以看到他的影子。
UITableView 中没有只有行,没有列。主要是在手机界面中多列不利于操作。
UITableView 的两种风格
UITableViewStylePlain 和 UITableViewStyleGrouped 。 两者操作没有本质的区别,只是显示的样式有点区别:

这两种样式的选择:
只有头部,或没有头部的组可以用Plain,有头部有尾部,或者组与组之间的间隙要很宽的时候,选择Grounped。
重要细节
Plain 方式将组推至到顶部的时候,组名会被一直阻塞在最边上,类似qq的好友分类。
查看 UITableView 的帮助文档,可以了解到 tableView 有代理: dataSources 和 delegate。
dataSources:主要是为 UITableView 提供显示用的数据,指定 UITableViewCell 支持的编辑操作类型(插入,删除,排序),并
更具用户操作继续数据的更新。
在数据没有更新的情况下可能存在异常。
delegate:主要提供一些可选的方法,来控制 tableView 的选择,指定 section 的头和尾以及协助完成删除和排序等功能。
1、UITableViewController
UITableViewController 是系统提供的一个便利类,主要是为了我们方便使用 UITableView,这个类在生成的时就已经设置好了 dataSource 和 delegate ,还包含一些代理函数的框架。
UITableViewController 的 tableView 是充满整个屏幕的。并且 tableView 和 view 就是一个对象
** 代码验证:
Snip20160602_34.png
当需要 tableView 不是充满整个屏幕的时候,我们可以直接在 UIViewController 中使用 UITableView
- UITableViewController 默认情况下会在 viewWillAppear 的时候,清空所有选中的 cell 。
设置self.clearsSelectionOnViewWillAppear = NO,来禁用该功能,并在viewDidAppear中调用UIScrollView的flashScrollIndicators方法让滚动条闪动一次,从而提示用户该控件是可以滑动的
2. UITableView
UITableView 继承自 UIScrollView。
UITableView 比较重要的两个东西:
- 数据源
- 代理
数据源
UITableView 的数据展示 —— 数据源
UITableView 的数据展示需要使用数据源,我们需要先设置数据源对象,实现数据源协议方法。tableView 会问数据源:
- 需要显示几组数据
- 每一组数据显示几行
- 每一行显示什么内容
UITableViewDataSource 方法
public protocol UITableViewDataSource : NSObjectProtocol {
//******************* 这三个方法是 tableView 显示数据的基本方法,实现这三个方法就可以显示 tableView 的内容 ************************
@available(iOS 2.0, *) // iOS 2.O 开始使用
// tableView 有多少行
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// 行显示的时候,通过设置 cell 的重用标识符,和调用 dequeueReusableCellWithIdentifier 方法来确认是否有可重用的 cell,尝试去重用 cell,
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
@available(iOS 2.0, *)
// 每一行 cell 显示什么内容
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
@available(iOS 2.0, *)
// tableView 有多少组 ( 这个方法是可选的 默认的实现返回时 1 ,不实现这个数据源方法,tableView 就显示一组数据)
optional public func numberOfSectionsInTableView(tableView: UITableView) -> Int // Default is 1 if not implemented
}
代理
UITableView 主要是使用代理来监听 TableView 的一些变化,给 tableView 设置一些值。
2、UITableViewCell
UITableView 中显示的每一个单元都是一个 UITableViewCell 对象。cell 引入的重用机制,让 tableView 滑动起来更加的流畅。
- 在 cell 创建的时候可以指定 cell 的样式。(有四种样式)
- 通过 cell 的 selectionStyle 属性设置 cell 的选中的风格。
通过 cell 的 accessoryType 来指定 cell 右边显示的内容。或者通过指定 accessoryType 来定制右边显示的 view
** 2.1 cell 的定制**
cell 不能满足我们的要求的时候,就需要定制 cell。
2.1.1 直接在 cell 的 contentView 中添加子控件。
2.1.2 UITableViewCell 派生子类
2、
UITableView 学习博客推荐
UITableView 的全面解析
UITableView学习笔记
