Skip to content

Latest commit

 

History

History
67 lines (42 loc) · 2.14 KB

UITableView in iOS.md

File metadata and controls

67 lines (42 loc) · 2.14 KB

UITableView in iOS

UITableViewCell

系统提供了四种UITableViewCell的样式,分别是

6jJfn1.md.png

如何使用cell的系统样式

两种方式

第一种方式是遵循正统的自定义cell套路

  1. 创建自定义cell,比如CustomCell,重写initWithStyle:reuseId初始化方法,在其中指定要用的style
  2. 通过UITbleview.register方法注册CustomCell
  3. cellForRow中通过dequeueCellWithIdForIndexPath获取cell,并为cell相应属性赋值

第二种方法比较古老一些

  1. 无需registercell
  2. 直接在cellForRow中通过dequeueCellWithId获取cell,紧跟着需要判断如果cell为空,则使用initWithStyle:reuseId新建一个cell
  3. 为cell相应的属性赋值

UITableViewCell.imageView

不论使用自定义cell还是系统样式的cell,imageView都存在与cell中,如果给它赋值

  • 它会显示
  • 并且默认情况下,cell分割线会右移

6jwsbj.md.png

cell之间的横线

  1. 当设置了seperator样式后,tableview的cell之间就会显示横线
  2. 但有个问题,多余的cell也会展示
  3. 解决办法是给tableviewFooter设置一个空的view

动画

  • insertRows等系列方法,默认情况是有动画地执行
  • reloadData则是无动画的刷新

tableviewHeader动画

可以通过如下代码实现tableViewHeader的动画

tableView.beginUpdates()
//header animation
tableView.endUpdates()

automaticDimension

当UITableView+isPagingEnable配合使用时,比如短视频App的大屏视频feed流页面,滚动过程中会发现contentSize不准确问题,原因在于UITableView.estimatedRowHeight属性

  • 该属性默认开启,值为automaticDimension
  • 因为了能每一行高度都不同,为了避免每次load开启后表示UIKit会通过该值估算contentSize等属性
  • 所以若需要精确的contentSize值,需要设置为0进行关闭

UITableViewDelegate

UITableViewCell

prepareForReuse和cellForRow不一定是成对出现的