只需要在你的項目中把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的時候,會形成一個死循環,那就是numberOfRowsInSection和heightForRowAtIndexPath方法不斷互調,最後程序崩潰.