今天重新对tableView 里面系统原生的数据方法执行顺序,进行一次梳理,主要说明下面4个方法的执行顺序
下面是swift的3个方法.oc的执行顺序一样.
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
执行全局刷新reloadData()之后
1.执行多次numberOfRowsInSection ,用来判断总共多少行
2.根据总共多少行,生成indexPath
根据indexPath,从 第0行开始到最后一行
然后执行一个循环根据indexPath执行方法
for indexPath in [indexPath]{
heightForRowAt (indexPath) // 执行多次,次数根据屏幕显示条数决定
cellForRowAt(indexPath)
heightForRowAt (indexPath) //,在执行完cellForRowAt以后,固定执行一次获取高度
}
执行2次heightForRowAt ,先取得多次第0行的高度
然后执行1次cellForRowAt,对第0行的cell进行创建和赋值操作
然后再执行1次heightForRowAt,对第0行进行赋值
知道了这样设计的原理以后,可以做到. 每次在执行cellForRowAt对 cell的 模型item进行赋值以后.可以在cell的item里面生成计算后的动态高度.然后给高度的数组进行赋值.然后再执行heightForRowAt的时候,在里面返回高度的数组,这样就可以让tableView每个cell的高度,根据cell自己计算以后,生成不同的高度,例如微信的朋友圈
执行局部刷新.reloadRows(at: [indexPath], with: .none)
局部刷新是只刷新 [indexPath]数组里面的行数,执行过程跟上面试一样的
for indexPath in [indexPath]{
heightForRowAt (indexPath) // 执行多次,次数根据屏幕显示条数决定
cellForRowAt(indexPath)
heightForRowAt (indexPath) //,在执行完cellForRowAt以后,固定执行一次获取高度
}