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()的调用过程,这一篇介绍的详细一点,大概的调用过程如下图所示. 其中浅红色的函数由主线程执行,而另外的浅绿色部分由另外一个线程执行,这个线程最终也会负责 ...
随机推荐
- RMQ with Shifts
uva12299:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- 利用低成本的MCU的UART驱动智能卡
在银行.身份识别和电信市场中,对安全和增强的功能性不断增长的需要,增加了全球范围智能卡的使用.另一方面,这也使得对安全性较低的磁条卡的使用量下降. 然而,所需的基于智能卡系统中,适当的通信系统的硬件和 ...
- android 随手记 广播通知栏 二
关于通知栏的使用: Notification及NotificationManager的使用详解 相关类: import android.app.NotificationManager; import ...
- Cut the Cake(大数相乘)
MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly o ...
- C#中实现邮件发送功能
public static int sendmail(string to, string body,string subject) { try { int nContain = 0; ///添加发件人 ...
- Pen Editor
Pen Editor
- CodeForce 7 B - Memory Manager(模拟)
题目大意:给你一段内存,要你进行如下的三个操作. 1.分配内存 alloc X ,分配连续一段长度为X的内存. 如果内存不够应该输出NULL,如果内存够就给这段内存标记一个编号. 2.擦除编号为 ...
- 图论:(Code Forces) Graph and String
Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Linux下动态库的使用
1.生成动态库: gcc -fPIC -shared -o libdemo.so demo.c 考虑程式库major的升级会破坏兼容性:而minior的升级则可能不会,一般建议用以下方式来生成动态库. ...
- Ancient Message (古埃及象形文字识别 Uva 1103)
原题:https://uva.onlinejudge.org/external/11/1103.pdf 给一幅图(16进制), 判断图中有哪些象形文字. 只识别 这6个就可以 示例: 将16进制数据 ...