tableView 刷新 抖動(dòng)解決辦法
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
這個(gè)方法是給tableview默認(rèn)加一個(gè)預(yù)估的cell值,在iOS11以下可以使用這個(gè)方法咒劲。里面返回UITableViewAutomaticDimension,如果在iOS11上出了reload閃屏诬滩,在創(chuàng)建tableview的時(shí)候使用
if(@avalible(IOS,*)){
tableview.estimatedRowHeight = 0
}
or
_tableView.estimatedSectionHeaderHeight = 0;
_tableView.estimatedSectionFooterHeight = 0;
_tableView.estimatedRowHeight = 0;
就行了產(chǎn)生的原因是在創(chuàng)建tablecell的時(shí)候系統(tǒng)給加了一個(gè)默認(rèn)預(yù)估的cell高度,每次reload都用這個(gè)高度計(jì)算cell,禁用或者設(shè)為0就行了
知識(shí)拓展:
- estimatedHeightForRowAtIndexPath:
-
在沒有實(shí)現(xiàn)estimatedHeightForRowAtIndexPath方法時(shí),這三個(gè)方法(numberOfRowsInSection,heightForRowAtIndexPath,cellForRowAtIndexPath)的調(diào)用順序如下:
1.先調(diào)用numberOfRowsInSection 2.再調(diào)用heightForRowAtIndexPath 3.再調(diào)用cellForRowAtIndexPath
-
實(shí)現(xiàn)了estimatedHeightForRowAtIndexPath方法后,調(diào)用順序是這樣的
1.numberOfRowsInSection 2.estimatedHeightForRowAtIndexPath 3.cellForRowAtIndexPath 4.heightForRowAtIndexPath
-
接下來我來說明以下出現(xiàn)EXC_BAD_ACCESS錯(cuò)誤的原因:
當(dāng)你沒用實(shí)現(xiàn)estimatedHeightForRowAtIndexPath方法時(shí)友驮, heightForRowAtIndexPath方法獲取cell的時(shí)候, 會(huì)形成一個(gè)死循環(huán),那就是numberOfRowsInSection和heightForRowAtIndexPath方法不斷互調(diào), 最后程序崩潰.