tableView代理方法的调用时间,(主要是heightForRowAtIndexPath和cellForRowAtIndexPath调用时间)
最近做一个demo,涉及按照数据分类然后依照分类在 cellForRowAtIndexPath形成不同类型的cell,就是有判断(在viewdidload里面做)和形成(在 cellForRowAtIndexPath做)两个阶段。这个时候我就有了一个疑问,viewdidload和 cellForRowAtIndexPath是什么顺序进行的,于是在几个函数里面记录打印了一下过程:

2015-03-03 13:14:20.970 whenToCom[9790:1381585] -[ContacterTableViewController viewDidLoad]
2015-03-03 13:14:21.167 whenToCom[9790:1381585] -[ContacterTableViewController viewWillAppear:]
2015-03-03 13:14:21.171 whenToCom[9790:1381585] -[ContacterTableViewController numberOfSectionsInTableView:]
2015-03-03 13:14:21.172 whenToCom[9790:1381585] -[ContacterTableViewController tableView:numberOfRowsInSection:]
2015-03-03 13:14:21.173 whenToCom[9790:1381585] -[ContacterTableViewController tableView:heightForRowAtIndexPath:]
2015-03-03 13:14:21.174 whenToCom[9790:1381585] -[ContacterTableViewController tableView:heightForRowAtIndexPath:]
2015-03-03 13:14:21.177 whenToCom[9790:1381585] -[ContacterTableViewController viewDidAppear:]
2015-03-03 13:14:21.181 whenToCom[9790:1381585] -[ContacterTableViewController numberOfSectionsInTableView:]
2015-03-03 13:14:21.182 whenToCom[9790:1381585] -[ContacterTableViewController tableView:numberOfRowsInSection:]
2015-03-03 13:14:21.195 whenToCom[9790:1381585] -[ContacterTableViewController tableView:heightForRowAtIndexPath:]
2015-03-03 13:14:21.197 whenToCom[9790:1381585] -[ContacterTableViewController tableView:heightForRowAtIndexPath:]
2015-03-03 13:14:21.199 whenToCom[9790:1381585] -[ContacterTableViewController tableView:cellForRowAtIndexPath:]
2015-03-03 13:14:21.206 whenToCom[9790:1381585] -[ContacterTableViewController tableView:heightForRowAtIndexPath:]
2015-03-03 13:14:21.208 whenToCom[9790:1381585] -[ContacterTableViewController tableView:cellForRowAtIndexPath:]
2015-03-03 13:14:21.211 whenToCom[9790:1381585] -[ContacterTableViewController tableView:heightForRowAtIndexPath:]

可以发现 tableView的cellForRowAtIndexPath是在viewdidappear后才执行的。
但是,又有了第二个疑问,我这个table里面有两条数据,但是heightForRowAtIndexPath方法为什么却执行了6次,而且 只有最后两次才是与cellForRowAtIndexPath交互执行,也就是说真正才是在绘制cell。在网上搜索了半天没结论,参考了一些类似的问 题,猜测是在viewWillAppear和viewDidAppear之间的两次调用是用于预估tableView的高度,紧跟 viewDidAppear之后的两次是实际计算tableView的高度,最后两次是绘制cell。
顺便PS一下View的生命周期的区别(原链接):
当一个视图控制器被创建,并在屏幕上显示的时候。 代码的执行顺序
1、 alloc 创建对象,分配空间
2、init (initWithNibName) 初始化对象,初始化数据
3、loadView 从nib载入视图 ,通常这一步不需要去干涉。除非你没有使用xib文件创建视图
4、viewDidLoad 载入完成,可以进行自定义数据以及动态创建其他控件
5、viewWillAppear 视图将出现在屏幕之前,马上这个视图就会被展现在屏幕上了
6、viewDidAppear 视图已在屏幕上渲染完成
当一个视图被移除屏幕并且销毁的时候的执行顺序,这个顺序差不多和上面的相反
1、viewWillDisappear 视图将被从屏幕上移除之前执行
2、viewDidDisappear 视图已经被从屏幕上移除,用户看不到这个视图了
3、dealloc 视图被销毁,此处需要对你在init和viewDidLoad中创建的对象进行释放
tableView代理方法的调用时间,(主要是heightForRowAtIndexPath和cellForRowAtIndexPath调用时间)的更多相关文章
- tableView代理方法执行顺序
tableView代理方法执行顺序,随着iOS系统版本的不断升级,执行顺序也有所变化 1.iOS7.1中先依次调一遍heightForRow方法再依次调一遍cellForRow方法,在调cellFor ...
- tableview 代理方法详解
typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) { UITableViewCellAccessoryNone, // 不显示任何图标 ...
- ios开发网络学八:NSURLSession相关代理方法
#import "ViewController.h" @interface ViewController ()<NSURLSessionDataDelegate> /* ...
- ios文本框基本使用,以及所有代理方法的作用
/* UITextField文本输入框 */ UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50 ...
- ios UITextField文本框基本使用,以及所有代理方法的作用
/* UITextField文本输入框 */ UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50 ...
- IOS中tableView每组的头部控件、通过tableView的代理方法控制某一行的cell能否达到高亮选中状态
一.tableView每组的头部控件 1.控件宽度默认就是tableView的宽度 2.控件高度由下面的代理方法决定 - (CGFloat)tableView:(UITableView *)table ...
- CAAnimationDelegate 代理方法没调用
CAAnimationDelegate 代理方法没调用 应该在 addAnimation调用之前设置代理
- 第4篇-JVM终于开始调用Java主类的main()方法啦
在前一篇 第3篇-CallStub新栈帧的创建 中我们介绍了generate_call_stub()函数的部分实现,完成了向CallStub栈帧中压入参数的操作,此时的状态如下图所示. 继续看gene ...
- 第29篇-调用Java主类的main()方法
在第1篇中大概介绍过Java中主类方法main()的调用过程,这一篇介绍的详细一点,大概的调用过程如下图所示. 其中浅红色的函数由主线程执行,而另外的浅绿色部分由另外一个线程执行,这个线程最终也会负责 ...
随机推荐
- Asynchronous
http://sporto.github.io/blog/2012/12/09/callbacks-listeners-promises/ http://www.cnblogs.com/liuning ...
- unity3d 幻灯片效果实现
上一篇使用的是静态方式进行的加载,采用的数据结构为 数组 该篇文章则是使用动态加载的方式实现: this.objsOfRouses = Resources.LoadAll("images&q ...
- STL容器介绍(转)
STL的容器可以分为以下几个大类: 一:序列容器, 有vector, list, deque, string. 二 : 关联容器, 有set, multiset, map, mulmap, h ...
- VS2012中使用编译的Qt-5.1.1静态库开发程序
1. 安装qt-vs-addin-1.2.2-opensource.exe,安装之后,vs2012菜单栏就会出现QT5菜单. 2. 在qt-vs-addin添加Qt5静态库: QT5->Qt O ...
- a trick in reading and storing file in the exact way!
read and write file is a very common operation regarding file mainuplation. However, the powerfull g ...
- 数据结构(Splay平衡树):COGS 339. [NOI2005] 维护数列
339. [NOI2005] 维护数列 时间限制:3 s 内存限制:256 MB [问题描述] 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际 ...
- 【模拟】Codeforces 691B s-palindrome
题目链接: http://codeforces.com/problemset/problem/691/B 题目大意: 求一个字符串是不是镜像的(不是回文).是输出TAK否则RE. 题目思路: [模拟] ...
- Search for a Range ——LeetCode
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 数据结构——N皇后放置方法种数
Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合 ...
- Cleaning Shifts(区间覆盖)
/* http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1019&ojid=1&cid=10 题目: 给定一个时 ...