为了避免保留循环,将使用[weak self] in
在 UITableViewCell 按钮操作中是否有必要?例子:
在视图控制器中cellForRow
cell.buttonAction = { (cell) [weak self] in
self.someFunction()
}
在 TableViewCell 类中
var buttonAction: ((UITableViewCell) -> Void)?
@IBAction func buttonPressed(_ sender: Any) {
buttonAction?(self)
}
要考虑的关键是:
var buttonAction: ((UITableViewCell) -> Void)?
您正在提供存储一个函数长期在实例属性中。
现在想想谁引用/拥有谁。视图控制器拥有它的视图,该视图是或拥有拥有单元格的表视图。同时细胞也拥有这个功能。如果该函数强烈引用我刚才提到的任何对象,这是一个保留周期。它是一个classic保留周期,保留周期如何形成的绝对模型。
[I would like to add a word about how I test for these things. There's a really cheap and easy way: wrap your view controller up in a navigation controller plus a blank root view controller, so that you can push your view controller onto it. Implement deinit
in your view controller. Now run the app, push your view controller, play with it for a bit, and pop it with the Back button. If deinit
isn't called, you've got a retain cycle.]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)