in the past couple of days I have been optimizing my app and found some memory leaks.
I have implemented a custom UIButton
that has a public event that I subscribe to. On one UITableViewController
the button is hosted in a UITableCellView
and I am subscribing to this event:
Cell:
class SelectionButtonCell : UITableViewCell
{
@IBOutlet weak var selectionButton: SelectionButton!
@IBOutlet weak var label: UILabel!
}
UITableViewController:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let selectionButtonCell = tableView.dequeueReusableCell(withIdentifier: "selectionButtonCell") as! SelectionButtonCell
selectionButtonCell.selectionButton.clicked = // This leaks memory because nowhere in my code this is set to nil
{
// Some unimportant stuff.
}
return selectionButtonCell
}
Button:
public class SelectionButton : UIButton
{
// MARK: - Events
public var clicked: (() -> ())?
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
{
super.touchesEnded(touches, with: event)
self.clicked?()
}
}
How do I set this var back to nil? I did not find any function that will be called when navigating back (the UITableViewController is pushed onto a UINavigationController
).
Is it okay to simply iterate over all cells and set this back to nil in viewWillDisappear(_)
or is there a nicer way?
I tried tableView:didEndDisplayingCell:forRowAtIndexPath
but it seems that this is only being called when scrolling the UITableView
.