这个其实是老生常谈了。这里不涉及cell重用机制,以及渲染(layer等)优化技巧。只是针对变高,这一个变量来说,怎么能提高页面的加载速度。
当然,针对几十个cell的情况,根本无需考虑怎么优化,或者上百的定高cell也不用考虑他的速度,原来的机制肯定是感觉上可以接受的。
那么在遇到大量cell的时候,它的高度计算,到底是在什么时候计算是最合适的,对于使用体验是最好的,由于iOS的SDK几经升级,机制也在不断的变化中。
其实就这俩函数:cellForRow,heightForRow
最初自己刚接触iOS开发时(大概在2010年iOS4),做过研究试验,他们的执行顺序是 先heightForRow,然后是cellForRow,并且呢,heightForRow是全部循环执行完毕,然后再执行可见的cellForRow,也就是说 比方有100个cell数据源,那么首先执行100次heightForRow,然后执行10个可见的cellForRow。这也就侧面说明:为什么已进入这个100个cell的页面,tableview的滚动指示条能有一个比较正确的比例和位置,因为机制就是 一进来,先算完所有cell的高度,计算完毕tableview的contentsize,此时就能知道滚动条的比例以及位置了。
但是现在,这个机制变化了,变成先执行cellForRow,然后执行heightForRow,而且是交替进行的。这个也算是一个进步,你想呀:cell的高度肯定是布局完毕的时候才能得到最精确的高度,此时再告诉tableview你的contentsize需要变化这么个数字。但是,并不是有多少数据源就一下子执行多少次,而是只针对可见cell执行这么多次,其余的等到被可见的时候再去执行。其实,之前有一段时间,具体哪个版本不记得了,只记得机制是有点混乱的,我都搞不清了:好像是heightForRow会执行2遍,cellForRow执行一遍。而且有的版本是一次性执行完毕所有数据源的height。
到后来iOS7的时候好像出现了一个estimatedHeightForRow这么个函数,说是为了提高tableview的初始化速度来的。由于老代码,按老的机制做了一点自己的优化,针对上百的cell加载时,速度也不是不能忍受,所以也就没动干戈。现在想弄明白这个玩意儿,到底怎么用,有没有效果。我做了几个试验,
准备200个变高cell,cell的高度随机设置,就是rand函数了。还有注意怎么srand以获得更为自然的随机高度。
第一次,不重写estimatedHeightForRow。也就是按老的机制,把heightForRow和cellForRow都重写。
每个函数打印log,得到如下
2020-02-29 14:07:11.640830+0800 tabtl2[25318:1714869] section cnt 1
2020-02-29 14:07:11.641950+0800 tabtl2[25318:1714869] cell rows cnt 100
2020-02-29 14:07:11.855857+0800 tabtl2[25318:1714869] section cnt 1
2020-02-29 14:07:11.856208+0800 tabtl2[25318:1714869] cell rows cnt 100
2020-02-29 14:07:11.856711+0800 tabtl2[25318:1714869] cell for 0
2020-02-29 14:07:11.910411+0800 tabtl2[25318:1714869] cell height row 0---height 80
2020-02-29 14:07:11.911979+0800 tabtl2[25318:1714869] cell for 1
2020-02-29 14:07:11.915514+0800 tabtl2[25318:1714869] cell height row 1---height 66
2020-02-29 14:07:11.916267+0800 tabtl2[25318:1714869] cell for 2
2020-02-29 14:07:11.917763+0800 tabtl2[25318:1714869] cell height row 2---height 6
2020-02-29 14:07:11.918435+0800 tabtl2[25318:1714869] cell for 3
2020-02-29 14:07:11.919855+0800 tabtl2[25318:1714869] cell height row 3---height 90
2020-02-29 14:07:11.920484+0800 tabtl2[25318:1714869] cell for 4
2020-02-29 14:07:11.944798+0800 tabtl2[25318:1714869] cell height row 4---height 68
2020-02-29 14:07:11.945393+0800 tabtl2[25318:1714869] cell for 5
2020-02-29 14:07:11.946323+0800 tabtl2[25318:1714869] cell height row 5---height 12
2020-02-29 14:07:11.946772+0800 tabtl2[25318:1714869] cell for 6
2020-02-29 14:07:11.947768+0800 tabtl2[25318:1714869] cell height row 6---height 78
2020-02-29 14:07:11.948253+0800 tabtl2[25318:1714869] cell for 7
2020-02-29 14:07:11.949269+0800 tabtl2[25318:1714869] cell height row 7---height 34
2020-02-29 14:07:11.975068+0800 tabtl2[25318:1714869] cell for 8
2020-02-29 14:07:11.976623+0800 tabtl2[25318:1714869] cell height row 8---height 17
2020-02-29 14:07:11.977229+0800 tabtl2[25318:1714869] cell for 9
2020-02-29 14:07:11.978420+0800 tabtl2[25318:1714869] cell height row 9---height 63
2020-02-29 14:07:11.978996+0800 tabtl2[25318:1714869] cell for 10
2020-02-29 14:07:11.980246+0800 tabtl2[25318:1714869] cell height row 10---height 86
2020-02-29 14:07:11.980899+0800 tabtl2[25318:1714869] cell for 11
2020-02-29 14:07:12.017665+0800 tabtl2[25318:1714869] cell height row 11---height 53
2020-02-29 14:07:12.018687+0800 tabtl2[25318:1714869] cell for 12
2020-02-29 14:07:12.020265+0800 tabtl2[25318:1714869] cell height row 12---height 56
2020-02-29 14:07:12.020909+0800 tabtl2[25318:1714869] cell for 13
2020-02-29 14:07:12.022246+0800 tabtl2[25318:1714869] cell height row 13---height 69
可见,目前iOS13时的机制是cellForRow和heightForRow都是针对可见cell执行,而且是交替执行,这已经是最优的办法了。
那如果加上那个所谓的estimatedHeightForRow,是什么情况呢?继续看log
2020-02-29 14:10:47.907364+0800 tabtl2[25335:1715911] section cnt 1
2020-02-29 14:10:47.908074+0800 tabtl2[25335:1715911] cell rows cnt 100
2020-02-29 14:10:47.908223+0800 tabtl2[25335:1715911] estimatedHeightForRow row 0
2020-02-29 14:10:47.908294+0800 tabtl2[25335:1715911] estimatedHeightForRow row 1
2020-02-29 14:10:47.908355+0800 tabtl2[25335:1715911] estimatedHeightForRow row 2
2020-02-29 14:10:47.908410+0800 tabtl2[25335:1715911] estimatedHeightForRow row 3
2020-02-29 14:10:47.908461+0800 tabtl2[25335:1715911] estimatedHeightForRow row 4
2020-02-29 14:10:47.908515+0800 tabtl2[25335:1715911] estimatedHeightForRow row 5
2020-02-29 14:10:47.908568+0800 tabtl2[25335:1715911] estimatedHeightForRow row 6
2020-02-29 14:10:47.908618+0800 tabtl2[25335:1715911] estimatedHeightForRow row 7
2020-02-29 14:10:47.908669+0800 tabtl2[25335:1715911] estimatedHeightForRow row 8
2020-02-29 14:10:47.908827+0800 tabtl2[25335:1715911] estimatedHeightForRow row 9
2020-02-29 14:10:47.909100+0800 tabtl2[25335:1715911] estimatedHeightForRow row 10
2020-02-29 14:10:47.909366+0800 tabtl2[25335:1715911] estimatedHeightForRow row 11
2020-02-29 14:10:47.909641+0800 tabtl2[25335:1715911] estimatedHeightForRow row 12
2020-02-29 14:10:47.909721+0800 tabtl2[25335:1715911] estimatedHeightForRow row 13
2020-02-29 14:10:47.909848+0800 tabtl2[25335:1715911] estimatedHeightForRow row 14
2020-02-29 14:10:47.909949+0800 tabtl2[25335:1715911] estimatedHeightForRow row 15
2020-02-29 14:10:47.910083+0800 tabtl2[25335:1715911] estimatedHeightForRow row 16
2020-02-29 14:10:47.910249+0800 tabtl2[25335:1715911] estimatedHeightForRow row 17
2020-02-29 14:10:47.910429+0800 tabtl2[25335:1715911] estimatedHeightForRow row 18
2020-02-29 14:10:47.910520+0800 tabtl2[25335:1715911] estimatedHeightForRow row 19
2020-02-29 14:10:47.910668+0800 tabtl2[25335:1715911] estimatedHeightForRow row 20
2020-02-29 14:10:47.910816+0800 tabtl2[25335:1715911] estimatedHeightForRow row 21
2020-02-29 14:10:47.910946+0800 tabtl2[25335:1715911] estimatedHeightForRow row 22
2020-02-29 14:10:47.911097+0800 tabtl2[25335:1715911] estimatedHeightForRow row 23
2020-02-29 14:10:47.911223+0800 tabtl2[25335:1715911] estimatedHeightForRow row 24
2020-02-29 14:10:47.911357+0800 tabtl2[25335:1715911] estimatedHeightForRow row 25
2020-02-29 14:10:47.911500+0800 tabtl2[25335:1715911] estimatedHeightForRow row 26
2020-02-29 14:10:47.911709+0800 tabtl2[25335:1715911] estimatedHeightForRow row 27
2020-02-29 14:10:47.911879+0800 tabtl2[25335:1715911] estimatedHeightForRow row 28
2020-02-29 14:10:47.912056+0800 tabtl2[25335:1715911] estimatedHeightForRow row 29
2020-02-29 14:10:47.912247+0800 tabtl2[25335:1715911] estimatedHeightForRow row 30
2020-02-29 14:10:47.912428+0800 tabtl2[25335:1715911] estimatedHeightForRow row 31
2020-02-29 14:10:47.912591+0800 tabtl2[25335:1715911] estimatedHeightForRow row 32
2020-02-29 14:10:47.912780+0800 tabtl2[25335:1715911] estimatedHeightForRow row 33
2020-02-29 14:10:47.912948+0800 tabtl2[25335:1715911] estimatedHeightForRow row 34
2020-02-29 14:10:47.913095+0800 tabtl2[25335:1715911] estimatedHeightForRow row 35
2020-02-29 14:10:47.913227+0800 tabtl2[25335:1715911] estimatedHeightForRow row 36
2020-02-29 14:10:47.913468+0800 tabtl2[25335:1715911] estimatedHeightForRow row 37
2020-02-29 14:10:47.913576+0800 tabtl2[25335:1715911] estimatedHeightForRow row 38
2020-02-29 14:10:47.913788+0800 tabtl2[25335:1715911] estimatedHeightForRow row 39
2020-02-29 14:10:47.913980+0800 tabtl2[25335:1715911] estimatedHeightForRow row 40
2020-02-29 14:10:47.914135+0800 tabtl2[25335:1715911] estimatedHeightForRow row 41
2020-02-29 14:10:47.914285+0800 tabtl2[25335:1715911] estimatedHeightForRow row 42
2020-02-29 14:10:47.914435+0800 tabtl2[25335:1715911] estimatedHeightForRow row 43
2020-02-29 14:10:47.914633+0800 tabtl2[25335:1715911] estimatedHeightForRow row 44
2020-02-29 14:10:47.914795+0800 tabtl2[25335:1715911] estimatedHeightForRow row 45
2020-02-29 14:10:47.915004+0800 tabtl2[25335:1715911] estimatedHeightForRow row 46
2020-02-29 14:10:47.915206+0800 tabtl2[25335:1715911] estimatedHeightForRow row 47
2020-02-29 14:10:47.915377+0800 tabtl2[25335:1715911] estimatedHeightForRow row 48
2020-02-29 14:10:47.915536+0800 tabtl2[25335:1715911] estimatedHeightForRow row 49
2020-02-29 14:10:47.915739+0800 tabtl2[25335:1715911] estimatedHeightForRow row 50
2020-02-29 14:10:47.915900+0800 tabtl2[25335:1715911] estimatedHeightForRow row 51
2020-02-29 14:10:47.916065+0800 tabtl2[25335:1715911] estimatedHeightForRow row 52
2020-02-29 14:10:47.916255+0800 tabtl2[25335:1715911] estimatedHeightForRow row 53
2020-02-29 14:10:47.916379+0800 tabtl2[25335:1715911] estimatedHeightForRow row 54
2020-02-29 14:10:47.916580+0800 tabtl2[25335:1715911] estimatedHeightForRow row 55
2020-02-29 14:10:47.916752+0800 tabtl2[25335:1715911] estimatedHeightForRow row 56
2020-02-29 14:10:47.916910+0800 tabtl2[25335:1715911] estimatedHeightForRow row 57
2020-02-29 14:10:47.917039+0800 tabtl2[25335:1715911] estimatedHeightForRow row 58
2020-02-29 14:10:47.917204+0800 tabtl2[25335:1715911] estimatedHeightForRow row 59
2020-02-29 14:10:47.917443+0800 tabtl2[25335:1715911] estimatedHeightForRow row 60
2020-02-29 14:10:47.917635+0800 tabtl2[25335:1715911] estimatedHeightForRow row 61
2020-02-29 14:10:47.917798+0800 tabtl2[25335:1715911] estimatedHeightForRow row 62
2020-02-29 14:10:47.918046+0800 tabtl2[25335:1715911] estimatedHeightForRow row 63
2020-02-29 14:10:47.918156+0800 tabtl2[25335:1715911] estimatedHeightForRow row 64
2020-02-29 14:10:47.918318+0800 tabtl2[25335:1715911] estimatedHeightForRow row 65
2020-02-29 14:10:47.918500+0800 tabtl2[25335:1715911] estimatedHeightForRow row 66
2020-02-29 14:10:47.918670+0800 tabtl2[25335:1715911] estimatedHeightForRow row 67
2020-02-29 14:10:47.918872+0800 tabtl2[25335:1715911] estimatedHeightForRow row 68
2020-02-29 14:10:47.919109+0800 tabtl2[25335:1715911] estimatedHeightForRow row 69
2020-02-29 14:10:47.919351+0800 tabtl2[25335:1715911] estimatedHeightForRow row 70
2020-02-29 14:10:47.919448+0800 tabtl2[25335:1715911] estimatedHeightForRow row 71
2020-02-29 14:10:47.919532+0800 tabtl2[25335:1715911] estimatedHeightForRow row 72
2020-02-29 14:10:47.919726+0800 tabtl2[25335:1715911] estimatedHeightForRow row 73
2020-02-29 14:10:47.920048+0800 tabtl2[25335:1715911] estimatedHeightForRow row 74
2020-02-29 14:10:47.920137+0800 tabtl2[25335:1715911] estimatedHeightForRow row 75
2020-02-29 14:10:47.920235+0800 tabtl2[25335:1715911] estimatedHeightForRow row 76
2020-02-29 14:10:47.920375+0800 tabtl2[25335:1715911] estimatedHeightForRow row 77
2020-02-29 14:10:47.920581+0800 tabtl2[25335:1715911] estimatedHeightForRow row 78
2020-02-29 14:10:47.920841+0800 tabtl2[25335:1715911] estimatedHeightForRow row 79
2020-02-29 14:10:47.920994+0800 tabtl2[25335:1715911] estimatedHeightForRow row 80
2020-02-29 14:10:47.921159+0800 tabtl2[25335:1715911] estimatedHeightForRow row 81
2020-02-29 14:10:47.921353+0800 tabtl2[25335:1715911] estimatedHeightForRow row 82
2020-02-29 14:10:47.921476+0800 tabtl2[25335:1715911] estimatedHeightForRow row 83
2020-02-29 14:10:47.921660+0800 tabtl2[25335:1715911] estimatedHeightForRow row 84
2020-02-29 14:10:47.921863+0800 tabtl2[25335:1715911] estimatedHeightForRow row 85
2020-02-29 14:10:47.922023+0800 tabtl2[25335:1715911] estimatedHeightForRow row 86
2020-02-29 14:10:47.922178+0800 tabtl2[25335:1715911] estimatedHeightForRow row 87
2020-02-29 14:10:47.922329+0800 tabtl2[25335:1715911] estimatedHeightForRow row 88
2020-02-29 14:10:47.922499+0800 tabtl2[25335:1715911] estimatedHeightForRow row 89
2020-02-29 14:10:47.922760+0800 tabtl2[25335:1715911] estimatedHeightForRow row 90
2020-02-29 14:10:47.922895+0800 tabtl2[25335:1715911] estimatedHeightForRow row 91
2020-02-29 14:10:47.923054+0800 tabtl2[25335:1715911] estimatedHeightForRow row 92
2020-02-29 14:10:47.923161+0800 tabtl2[25335:1715911] estimatedHeightForRow row 93
2020-02-29 14:10:47.923420+0800 tabtl2[25335:1715911] estimatedHeightForRow row 94
2020-02-29 14:10:47.923497+0800 tabtl2[25335:1715911] estimatedHeightForRow row 95
2020-02-29 14:10:47.923620+0800 tabtl2[25335:1715911] estimatedHeightForRow row 96
2020-02-29 14:10:47.923741+0800 tabtl2[25335:1715911] estimatedHeightForRow row 97
2020-02-29 14:10:47.923983+0800 tabtl2[25335:1715911] estimatedHeightForRow row 98
2020-02-29 14:10:47.924050+0800 tabtl2[25335:1715911] estimatedHeightForRow row 99
2020-02-29 14:10:47.942532+0800 tabtl2[25335:1715911] section cnt 1
2020-02-29 14:10:47.942651+0800 tabtl2[25335:1715911] cell rows cnt 100
2020-02-29 14:10:47.942694+0800 tabtl2[25335:1715911] estimatedHeightForRow row 0
2020-02-29 14:10:47.942733+0800 tabtl2[25335:1715911] estimatedHeightForRow row 1
2020-02-29 14:10:47.942763+0800 tabtl2[25335:1715911] estimatedHeightForRow row 2
2020-02-29 14:10:47.942795+0800 tabtl2[25335:1715911] estimatedHeightForRow row 3
2020-02-29 14:10:47.942823+0800 tabtl2[25335:1715911] estimatedHeightForRow row 4
2020-02-29 14:10:47.942850+0800 tabtl2[25335:1715911] estimatedHeightForRow row 5
2020-02-29 14:10:47.942982+0800 tabtl2[25335:1715911] estimatedHeightForRow row 6
2020-02-29 14:10:47.943105+0800 tabtl2[25335:1715911] estimatedHeightForRow row 7
2020-02-29 14:10:47.943211+0800 tabtl2[25335:1715911] estimatedHeightForRow row 8
2020-02-29 14:10:47.943322+0800 tabtl2[25335:1715911] estimatedHeightForRow row 9
2020-02-29 14:10:47.943456+0800 tabtl2[25335:1715911] estimatedHeightForRow row 10
2020-02-29 14:10:47.943638+0800 tabtl2[25335:1715911] estimatedHeightForRow row 11
2020-02-29 14:10:47.943782+0800 tabtl2[25335:1715911] estimatedHeightForRow row 12
2020-02-29 14:10:47.943899+0800 tabtl2[25335:1715911] estimatedHeightForRow row 13
2020-02-29 14:10:47.944018+0800 tabtl2[25335:1715911] estimatedHeightForRow row 14
2020-02-29 14:10:47.944155+0800 tabtl2[25335:1715911] estimatedHeightForRow row 15
2020-02-29 14:10:47.944264+0800 tabtl2[25335:1715911] estimatedHeightForRow row 16
2020-02-29 14:10:47.944382+0800 tabtl2[25335:1715911] estimatedHeightForRow row 17
2020-02-29 14:10:47.944500+0800 tabtl2[25335:1715911] estimatedHeightForRow row 18
2020-02-29 14:10:47.944681+0800 tabtl2[25335:1715911] estimatedHeightForRow row 19
2020-02-29 14:10:47.944814+0800 tabtl2[25335:1715911] estimatedHeightForRow row 20
2020-02-29 14:10:47.944932+0800 tabtl2[25335:1715911] estimatedHeightForRow row 21
2020-02-29 14:10:47.945081+0800 tabtl2[25335:1715911] estimatedHeightForRow row 22
2020-02-29 14:10:47.945183+0800 tabtl2[25335:1715911] estimatedHeightForRow row 23
2020-02-29 14:10:47.945283+0800 tabtl2[25335:1715911] estimatedHeightForRow row 24
2020-02-29 14:10:47.945383+0800 tabtl2[25335:1715911] estimatedHeightForRow row 25
2020-02-29 14:10:47.945482+0800 tabtl2[25335:1715911] estimatedHeightForRow row 26
2020-02-29 14:10:47.945580+0800 tabtl2[25335:1715911] estimatedHeightForRow row 27
2020-02-29 14:10:47.945695+0800 tabtl2[25335:1715911] estimatedHeightForRow row 28
2020-02-29 14:10:47.945840+0800 tabtl2[25335:1715911] estimatedHeightForRow row 29
2020-02-29 14:10:47.945968+0800 tabtl2[25335:1715911] estimatedHeightForRow row 30
2020-02-29 14:10:47.946095+0800 tabtl2[25335:1715911] estimatedHeightForRow row 31
2020-02-29 14:10:47.946234+0800 tabtl2[25335:1715911] estimatedHeightForRow row 32
2020-02-29 14:10:47.946359+0800 tabtl2[25335:1715911] estimatedHeightForRow row 33
2020-02-29 14:10:47.946478+0800 tabtl2[25335:1715911] estimatedHeightForRow row 34
2020-02-29 14:10:47.946596+0800 tabtl2[25335:1715911] estimatedHeightForRow row 35
2020-02-29 14:10:47.946716+0800 tabtl2[25335:1715911] estimatedHeightForRow row 36
2020-02-29 14:10:47.946828+0800 tabtl2[25335:1715911] estimatedHeightForRow row 37
2020-02-29 14:10:47.947023+0800 tabtl2[25335:1715911] estimatedHeightForRow row 38
2020-02-29 14:10:47.947173+0800 tabtl2[25335:1715911] estimatedHeightForRow row 39
2020-02-29 14:10:47.947289+0800 tabtl2[25335:1715911] estimatedHeightForRow row 40
2020-02-29 14:10:47.947418+0800 tabtl2[25335:1715911] estimatedHeightForRow row 41
2020-02-29 14:10:47.947572+0800 tabtl2[25335:1715911] estimatedHeightForRow row 42
2020-02-29 14:10:47.947723+0800 tabtl2[25335:1715911] estimatedHeightForRow row 43
2020-02-29 14:10:47.947836+0800 tabtl2[25335:1715911] estimatedHeightForRow row 44
2020-02-29 14:10:47.947954+0800 tabtl2[25335:1715911] estimatedHeightForRow row 45
2020-02-29 14:10:47.948059+0800 tabtl2[25335:1715911] estimatedHeightForRow row 46
2020-02-29 14:10:47.948160+0800 tabtl2[25335:1715911] estimatedHeightForRow row 47
2020-02-29 14:10:47.948277+0800 tabtl2[25335:1715911] estimatedHeightForRow row 48
2020-02-29 14:10:47.948393+0800 tabtl2[25335:1715911] estimatedHeightForRow row 49
2020-02-29 14:10:47.948540+0800 tabtl2[25335:1715911] estimatedHeightForRow row 50
2020-02-29 14:10:47.948659+0800 tabtl2[25335:1715911] estimatedHeightForRow row 51
2020-02-29 14:10:47.948795+0800 tabtl2[25335:1715911] estimatedHeightForRow row 52
2020-02-29 14:10:47.948918+0800 tabtl2[25335:1715911] estimatedHeightForRow row 53
2020-02-29 14:10:47.949049+0800 tabtl2[25335:1715911] estimatedHeightForRow row 54
2020-02-29 14:10:47.949169+0800 tabtl2[25335:1715911] estimatedHeightForRow row 55
2020-02-29 14:10:47.949296+0800 tabtl2[25335:1715911] estimatedHeightForRow row 56
2020-02-29 14:10:47.949433+0800 tabtl2[25335:1715911] estimatedHeightForRow row 57
2020-02-29 14:10:47.949550+0800 tabtl2[25335:1715911] estimatedHeightForRow row 58
2020-02-29 14:10:47.949670+0800 tabtl2[25335:1715911] estimatedHeightForRow row 59
2020-02-29 14:10:47.949801+0800 tabtl2[25335:1715911] estimatedHeightForRow row 60
2020-02-29 14:10:47.949903+0800 tabtl2[25335:1715911] estimatedHeightForRow row 61
2020-02-29 14:10:47.950008+0800 tabtl2[25335:1715911] estimatedHeightForRow row 62
2020-02-29 14:10:47.950157+0800 tabtl2[25335:1715911] estimatedHeightForRow row 63
2020-02-29 14:10:47.950297+0800 tabtl2[25335:1715911] estimatedHeightForRow row 64
2020-02-29 14:10:47.950410+0800 tabtl2[25335:1715911] estimatedHeightForRow row 65
2020-02-29 14:10:47.950517+0800 tabtl2[25335:1715911] estimatedHeightForRow row 66
2020-02-29 14:10:47.950635+0800 tabtl2[25335:1715911] estimatedHeightForRow row 67
2020-02-29 14:10:47.950742+0800 tabtl2[25335:1715911] estimatedHeightForRow row 68
2020-02-29 14:10:47.950891+0800 tabtl2[25335:1715911] estimatedHeightForRow row 69
2020-02-29 14:10:47.951014+0800 tabtl2[25335:1715911] estimatedHeightForRow row 70
2020-02-29 14:10:47.951141+0800 tabtl2[25335:1715911] estimatedHeightForRow row 71
2020-02-29 14:10:47.951287+0800 tabtl2[25335:1715911] estimatedHeightForRow row 72
2020-02-29 14:10:47.951431+0800 tabtl2[25335:1715911] estimatedHeightForRow row 73
2020-02-29 14:10:47.951584+0800 tabtl2[25335:1715911] estimatedHeightForRow row 74
2020-02-29 14:10:47.951711+0800 tabtl2[25335:1715911] estimatedHeightForRow row 75
2020-02-29 14:10:47.951842+0800 tabtl2[25335:1715911] estimatedHeightForRow row 76
2020-02-29 14:10:47.951987+0800 tabtl2[25335:1715911] estimatedHeightForRow row 77
2020-02-29 14:10:47.952117+0800 tabtl2[25335:1715911] estimatedHeightForRow row 78
2020-02-29 14:10:47.952262+0800 tabtl2[25335:1715911] estimatedHeightForRow row 79
2020-02-29 14:10:47.952371+0800 tabtl2[25335:1715911] estimatedHeightForRow row 80
2020-02-29 14:10:47.952505+0800 tabtl2[25335:1715911] estimatedHeightForRow row 81
2020-02-29 14:10:47.952638+0800 tabtl2[25335:1715911] estimatedHeightForRow row 82
2020-02-29 14:10:47.952767+0800 tabtl2[25335:1715911] estimatedHeightForRow row 83
2020-02-29 14:10:47.952895+0800 tabtl2[25335:1715911] estimatedHeightForRow row 84
2020-02-29 14:10:47.953023+0800 tabtl2[25335:1715911] estimatedHeightForRow row 85
2020-02-29 14:10:47.953123+0800 tabtl2[25335:1715911] estimatedHeightForRow row 86
2020-02-29 14:10:47.953226+0800 tabtl2[25335:1715911] estimatedHeightForRow row 87
2020-02-29 14:10:47.953374+0800 tabtl2[25335:1715911] estimatedHeightForRow row 88
2020-02-29 14:10:47.953478+0800 tabtl2[25335:1715911] estimatedHeightForRow row 89
2020-02-29 14:10:47.953601+0800 tabtl2[25335:1715911] estimatedHeightForRow row 90
2020-02-29 14:10:47.953705+0800 tabtl2[25335:1715911] estimatedHeightForRow row 91
2020-02-29 14:10:47.953851+0800 tabtl2[25335:1715911] estimatedHeightForRow row 92
2020-02-29 14:10:47.953968+0800 tabtl2[25335:1715911] estimatedHeightForRow row 93
2020-02-29 14:10:47.954073+0800 tabtl2[25335:1715911] estimatedHeightForRow row 94
2020-02-29 14:10:47.954178+0800 tabtl2[25335:1715911] estimatedHeightForRow row 95
2020-02-29 14:10:47.954319+0800 tabtl2[25335:1715911] estimatedHeightForRow row 96
2020-02-29 14:10:47.954443+0800 tabtl2[25335:1715911] estimatedHeightForRow row 97
2020-02-29 14:10:47.954565+0800 tabtl2[25335:1715911] estimatedHeightForRow row 98
2020-02-29 14:10:47.954687+0800 tabtl2[25335:1715911] estimatedHeightForRow row 99
2020-02-29 14:10:47.954892+0800 tabtl2[25335:1715911] cell for 0
2020-02-29 14:10:47.956416+0800 tabtl2[25335:1715911] cell height row 0---height 80
2020-02-29 14:10:47.956813+0800 tabtl2[25335:1715911] cell for 1
2020-02-29 14:10:47.957577+0800 tabtl2[25335:1715911] cell height row 1---height 7
2020-02-29 14:10:47.957834+0800 tabtl2[25335:1715911] cell for 2
2020-02-29 14:10:47.958371+0800 tabtl2[25335:1715911] cell height row 2---height 27
2020-02-29 14:10:47.958599+0800 tabtl2[25335:1715911] cell for 3
2020-02-29 14:10:47.959094+0800 tabtl2[25335:1715911] cell height row 3---height 97
2020-02-29 14:10:47.959310+0800 tabtl2[25335:1715911] cell for 4
2020-02-29 14:10:47.959771+0800 tabtl2[25335:1715911] cell height row 4---height 76
2020-02-29 14:10:47.959988+0800 tabtl2[25335:1715911] cell for 5
2020-02-29 14:10:47.960463+0800 tabtl2[25335:1715911] cell height row 5---height 41
2020-02-29 14:10:47.960673+0800 tabtl2[25335:1715911] cell for 6
2020-02-29 14:10:47.961151+0800 tabtl2[25335:1715911] cell height row 6---height 94
2020-02-29 14:10:47.961352+0800 tabtl2[25335:1715911] cell for 7
2020-02-29 14:10:47.961803+0800 tabtl2[25335:1715911] cell height row 7---height 98
2020-02-29 14:10:47.962004+0800 tabtl2[25335:1715911] cell for 8
2020-02-29 14:10:47.962471+0800 tabtl2[25335:1715911] cell height row 8---height 90
2020-02-29 14:10:47.962689+0800 tabtl2[25335:1715911] cell for 9
2020-02-29 14:10:47.963145+0800 tabtl2[25335:1715911] cell height row 9---height 53
2020-02-29 14:10:47.963342+0800 tabtl2[25335:1715911] cell for 10
2020-02-29 14:10:47.963790+0800 tabtl2[25335:1715911] cell height row 10---height 70
2020-02-29 14:10:47.963988+0800 tabtl2[25335:1715911] cell for 11
2020-02-29 14:10:47.964353+0800 tabtl2[25335:1715911] cell height row 11---height 72
2020-02-29 14:10:47.964508+0800 tabtl2[25335:1715911] cell for 12
2020-02-29 14:10:47.964857+0800 tabtl2[25335:1715911] cell height row 12---height 5
2020-02-29 14:10:47.965020+0800 tabtl2[25335:1715911] cell for 13
2020-02-29 14:10:47.965342+0800 tabtl2[25335:1715911] cell height row 13---height 16
2020-02-29 14:10:47.965492+0800 tabtl2[25335:1715911] cell for 14
2020-02-29 14:10:47.965821+0800 tabtl2[25335:1715911] cell height row 14---height 55
2020-02-29 14:10:47.965965+0800 tabtl2[25335:1715911] cell for 15
2020-02-29 14:10:47.966299+0800 tabtl2[25335:1715911] cell height row 15---height 95
它居然一下子执行了100次,天啊,结合它的注释“If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.”,我哪里还敢把expensive logic place 到here。根本没有什么卵用。函数该执行多少还是执行多少次,该怎么耗时还是怎么耗时。
所以,那个新的函数没什么卵用。那到底怎么提高加载体验呢,我是这么做的
第一次进入页面,肯定是要牺牲一点体验的,就是要计算cell的高度,并且这个计算也是延迟计算,按需计算,有100个cell,但是回调函数只需要显示10个cell,那么我就在回调函数里计算这10个cell,但是这个值我并不是用完就丢了,而是把它存起来,下次再来,我就不用计算了,直接拿来用,是不是就省时间了呢,至于存在哪里,存多久,这个就看你的需求了。可以存在内存里,与页面生命周期一致,那么上下滑动的时候重复获取就行了,不用计算。也可以存在磁盘上,存在对应数据的某个属性里,这样,即便页面的生命周期结束了,下次再进来,直接从磁盘读过来,肯定比现场计算快得多,但是这个值需要刷新,因为不同的sdk时,这个页面可能会有少许不同比方字体不一样了。这个时候就需要重新计算和存储。
我本来想优化现有的老代码的机制,用上sdk的新特性,研究了一下,没什么卵用,干脆不用了。
可能我了解的还是少,到底这个特性(不算新了,iOS7就有了)怎么用才对呢。