iOS学习之UITableView

UITableViewCell复用机制

UITableView可能要显示很多条数据,但屏幕尺寸有限,所以只能展示一部分,系统只需要创建这一部分数量的cell对象即可。
当手指从下向上滑动时,回收屏幕外最上方的item,并放置到列表的最下方。
手指由下向上滑动同理。
dequeueReusableCell方法的作用是从单元格对象池中获取指定类型并可复用的单元格对象。
如果从对象池中没有获取到可复用的item,就创建一个。

UITableView的功能

UITableView为我们提供了许多功能,如选择、删除、移动、索引子项等。这些功能的实现方法都集成在UITableViewDelegate和UITableViewDataSource中。

这里我们先提取出所有tableview都必须实现的方法

// 设置某一section中item的数量,必须实现
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
// 初始化和复用指定索引位置的Celln必须实现
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

添加索引

如同iPhone的通讯录一样,在右侧有一个索引序列。
索引需要用到的代理方法有以下几个

// 索引目录数量
func numberOfSections(in tableView: UITableView) -> Int 
// 索引目录标题
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
// 设置在列表右侧显示的索引指示器的内容,返回结果为一个字符串数组
func sectionIndexTitles(for tableView: UITableView) -> [String]? 

在init的时候,第二个参数有两个值:.plain和.grouped。

let tableView = UITableView(frame: view.bounds, style: .plain)

如果不添加章节头部的话,基本看不出这两个值给tableView带来的区别。

  • .plain: 向上滑动,当Section头部滑动到UITableVeiw的上方边界时,Section头部会停在边界位置,知道下一个章节头部到达它的位置,它才会继续向上滑动,下一个章节头部会占据它的位置。另外,如果cell的个数不够铺满屏幕,系统会一直创建空的cell来扑满。
  • .grouped: 正常滑动。

选择功能

实现选择需要的接口方法,

// indexPath选择当前item的位置
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
// indexPath当前item上一个被选择的的位置
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)

通过把cell的accessoryType属性设置成checkmark,然后调用reloadData(),来更新数据。
如果要实现单选,需要把之前选择的cell的该属性设置为none。

cell的accessoryType属性的值是枚举UITableViewCellAccessoryType:

枚举类型 说明
none 没有任何的样式
detailButton 右侧蓝色的圆圈,中间带叹号
detailDisclosureButton 右侧蓝色圆圈带叹号,它的右侧还有一个灰色向右的箭头
disclosureIndicator 右侧一个灰色的向右箭头
checkmark 右侧蓝色对号

插入与删除

需要用到的代理方法有

// 编辑模式 插入还是删除
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle
// 执行编辑操作时调用此方法 
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)

还有需要一个编辑模式的开关控制

// 第一个参数是否开启编辑状态,第二个参数是否有动画
// setEditing(_ editing:, animated:)

tableView.setEditing(!tableView.isEditing, animated: true)

UITableViewCell.EditingStyle有三种属性,含义如其名。

  • insert
  • delete
  • none

tableView(:, commit editingStyle:, forRowAt:)_,当执行了编辑操作,就会调起这个方法,你可以通过编辑状态对TableView和数据源进行操作。注意一定要把数据源和视图显示操作保持一致,不然很容易数组越界导致崩溃。在该方法中调用:

  • insertRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation)
  • deleteRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation)

作用是对TableViewCell的条数进行增加或删除操作。

移动/重排序

拖动改变位置的操作同样需要开启编辑模式

tableView.setEditing(!tableView.isEditing, animated: true)

// cell是否可以通过拖动改变其位置
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
// 每次移动结束后调用此方法,在这里进行移动后将数据源和视图同步的操作,第二个参数是cell移动前的位置,第三个参数是移动后的位置
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) 

你可能感兴趣的:(iOS学习之UITableView)