iOS之estimatedHeightForRowAtIndexPath避免程序EXC_BAD_ACCESS


在你的項目中把estimatedHeightForRowAtIndexPath方法寫實現以下,返回一個估計高度(隨便估,筆者建議還是按照正常思路來寫,大概高度是多少就返回多少),這樣就不會報EXC_BAD_ACCESS錯誤了.

注意:estimatedHeightForRowAtIndexPath方法既是下面這個方法.

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;

知識拓展:

  • 在沒有實現estimatedHeightForRowAtIndexPath方法時,這三個方法(numberOfRowsInSection,heightForRowAtIndexPath,cellForRowAtIndexPath)的調用順序如下:
    • 1.先調用numberOfRowsInSection
    • 2.再調用heightForRowAtIndexPath
    • 3.再調用cellForRowAtIndexPath
  • 實現了estimatedHeightForRowAtIndexPath方法后,調用順序是這樣的

    • 1.numberOfRowsInSection
    • 2.estimatedHeightForRowAtIndexPath
    • 3.cellForRowAtIndexPath
    • 4.heightForRowAtIndexPath
  • 接下來我來說明以下出現EXC_BAD_ACCESS錯誤的原因:

    • 當你沒用實現**estimatedHeightForRowAtIndexPath方法,在heightForRowAtIndexPath方法獲取cell的時候,會形成一個死循環,那就是numberOfRowsInSectionheightForRowAtIndexPath方法不斷互調,最后程序崩潰.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM