iOS-如何在heightForRowAtIndexPath获取cell又不会引起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方法不断互调,最后程序崩溃.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容