简体   繁体   中英

Retain Cycle in closure in CellView of TableView

I am facing retain cycle issue in my ViewController. I have added a closure in my tableview cell to listen click on button as:

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

and calling it as:

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

closure is implemented in cellForRow method as:

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?.

You should declare the var inside the cell as 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. That was causing reatain cycle in my class. Thanks for all answers on this question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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