Is weak self needed for table view cell button closure

后端 未结 3 994
忘掉有多难
忘掉有多难 2021-01-27 04:17

In trying to avoid retain cycles, would using [weak self] in in a UITableViewCell button action be necessary? Example:

in ViewController\'s cellForRow<

3条回答
  •  暖寄归人
    2021-01-27 05:17

    if you're not using the object of tableviewcell and you only want execute action on the cell make this.

    var buttonAction: (() -> Void)?
    
    @IBAction func buttonPressed(_ sender: Any) {
         buttonAction?()
    }
    

    and on the cellForRow use this,

    cell.buttonAction = { [weak self] in
         self?.someFunction()
    }
    

    for check retain cycles, I usually use this option for check if the memory is retain or not.

提交回复
热议问题