简体   繁体   English

Retain Cycle in closure in cellView of TableView

[英]Retain Cycle in closure in CellView of TableView

I am facing retain cycle issue in my ViewController.我在我的 ViewController 中面临保留周期问题。 I have added a closure in my tableview cell to listen click on button as:我在我的 tableview 单元格中添加了一个闭包来监听点击按钮:

   var onBtnActionClickHandler: (() -> ())?

and calling it as:并将其称为:

@objc func btnActionClicked() {
    onBtnActionClickHandler?()
}

closure is implemented in cellForRow method as:闭包在 cellForRow 方法中实现为:

cell.onBtnActionClickHandler = { [weak self] in
    self?.btnActionClicked()
}

but I am facing retain cycle in this process.但我在这个过程中面临着保留周期。 According to my understanding it should not have strong reference to have retain cycle.根据我的理解,它不应该有很强的引用来保留循环。 Can anyone explain what is wrong in this process.任何人都可以解释这个过程中出了什么问题。 Thanks谢谢

The var onBtnActionClickHandler is retained by the cell and a strong reference to your controller?. var onBtnActionClickHandler 由单元格保留,并强烈引用您的 controller?。

You should declare the var inside the cell as weak:您应该将单元格内的 var 声明为 weak:

weak var onBtnActionClickHandler: (() -> ())?

OK.好的。 So I have solved this issue.所以我已经解决了这个问题。 The problem was not with this cell, acctually a manager class was setting a listener twice and releasing it only once.问题不在于这个单元格,实际上是一个经理 class 两次设置了一个监听器并且只释放了一次。 That was causing reatain cycle in my class. Thanks for all answers on this question.那导致了我的 class 中的 reatain 循环。感谢您对这个问题的所有回答。

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

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