简体   繁体   English

iOS,带有观察者的自定义表格视图单元,如何分离观察者

[英]IOS, Custom Table View Cell with Observers, how to Detach Observers

So we have this Table View Custom Cell with it's own class. 因此,我们拥有该表视图自定义单元格和它自己的类。 In the class we are playing with putting Key Value Pair Observers. 在课堂上,我们正在玩放置键值对观察器。 Yes, we have reason for this... 是的,我们有这个理由...

Anyway, at certain times are are getting... 无论如何,在某些时候变得越来越...

An instance 0x7680410 of class CustomCellBase was deallocated while key value observers
were still registered with it. Observation info was leaked, and may even become mistakenly
attached to some other object.

This is of course due to the cells being "reused". 这当然是由于细胞被“重用”。

What would be the best way to clear these? 清除这些错误的最佳方法是什么? Is there a way? 有办法吗? Is it the case that we can not use Observers in Custom Table Cells? 是否无法在自定义表格单元格中使用观察者?

You need to stop observing the cell when it's removed from view. 将其从视图中移除后,您需要停止观察该单元。

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[NSNotificationCenter defaultCenter] removeObserver:cell];
}

Otherwise NSNotificationCenter will try to fire the selector on removed cell causing the error. 否则,NSNotificationCenter将尝试在删除的单元格上触发选择器,从而导致错误。 Of course you will need to re-register the cell when it appears. 当然,当单元出现时,您将需要重新注册。

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

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