за последние пару дней я оптимизировал свое приложение и обнаружил некоторые утечки памяти. Я реализовал пользовательский UIButton
, в котором есть событие publi c, на которое я подписываюсь. На одной UITableViewController
кнопка находится в UITableCellView
, и я подписываюсь на это событие:
Ячейка:
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
}
Кнопка:
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?()
}
}
Как мне установить эту переменную на ноль? Я не нашел ни одной функции, которая будет вызываться при переходе назад (UITableViewController помещается в UINavigationController
). Можно ли просто перебрать все ячейки и установить значение nil в viewWillDisappear(_)
или есть более хороший способ?
Я пробовал tableView:didEndDisplayingCell:forRowAtIndexPath
, но кажется, что это вызывается только при прокрутке UITableView
.