简体   繁体   English

iOS TableViewCell exc_bad_access自动释放错误

[英]IOS TableViewCell exc_bad_access autorelease error

I have a problem on the memory management of a tableviewcell ( exc_bad_access ). 我对tableviewcell( exc_bad_access )的内存管理有问题。
One of mine tableviewcell include a uiwebview that loads asynchronous html data ( loadHTMLString method) and the current controller is set as its delegate. 我的tableviewcell包括一个uiwebview,它加载异步html数据( loadHTMLString方法),并将当前控制器设置为其委托。
When i pop tableviewcontroller from my navigation controller too fast the app crashes. 当我从导航控制器中弹出tableviewcontroller时,应用崩溃。

In my opinion this is the reason why it fails: 我认为这是失败的原因:
- if cell deallocs before the controller => all ok, delegate is still live and i can even set its delegate to nil in its own dealloc method -如果单元格在控制器之前取消分配=>一切正常,则委托仍然有效,我什至可以在其自己的dealloc方法中将其委托设置为nil
- if cell deallocs after the controller (i guess because table view cells are autoreleased) => the app crashes because its delegate it's still set to the deallocated controller -如果单元格在控制器之后取消分配(我猜是因为表视图单元格是自动释放的)=>应用程序崩溃,因为它的委托仍然设置为释放的控制器

Any idea how to correctly solve this?? 任何想法如何正确解决这个问题? Thank you.. 谢谢..

CODE in cellForRowAtIndexPath: cellForRowAtIndexPath中的代码:

...
CustomTableViewCell * cCell = (CustomTableViewCell*)cell; 
cCell.myWebView.delegate = self; 
[[cCell myWebView] loadHTMLString:html baseURL:baseURL];
....

在释放Web视图之前,尝试在单元取消分配处添加stopLoading

This is an older question, but it never really got solved, and I just ran into it myself. 这是一个比较老的问题,但从未真正解决过,我自己就遇到了。

This happened to me: The view controller would crash when a cell was loaded with a web view who had the cell as it's delegate. 这发生在我身上:当单元加载有以该单元为委托的Web视图时,视图控制器将崩溃。 UIWebView's documentation states that a web view's delegate should be set to nil before it is deallocated. UIWebView的文档指出,在释放Web视图的委托之前,应将其设置为nil。 This seems to also be true for reused table view cells. 对于重用的表视图单元格似乎也是如此。

My solution: override these in your custom cell: 我的解决方案:在您的自定义单元中覆盖这些:

- (void)prepareForReuse
    {
    [super prepareForReuse];
    self.myWebView.delegate = nil;
    [self.myWebView stopLoading];
    }

- (void)dealloc
    {
    self.myWebView.delegate = nil;
    [self.myWebView stopLoading];
    }

This fixed it for me. 这为我解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM