1

I have used blocks in cell for getting switch value but now my problem is that deinit not called where i used the blocks. it is completely working previously but in swift 3.0 it is not working.

My switch cell :

import UIKit

class CellSwitch: UITableViewCell {

    @IBOutlet weak var objSwitch: UISwitch!
    @IBOutlet weak var btnInfo: UIButton!
    @IBOutlet weak var lblTitle: UILabel!
    var blockSwitch_Change : ((_ isOn:Bool) -> Void)!
    var blockBtn_Clicked : (() -> Void)!

    override func awakeFromNib() {
        super.awakeFromNib()
        self.lblTitle.font = Font.init(Font.FontType.custom(Font.FontName.NotoSans_Regular), size: Font.FontSize.standard(Font.StandardSize.Regular)).instance
        // Initialization code
    }
    //MARK:- switch object change
    @IBAction func switch_ValChanged(_ obj:UISwitch){
        self.blockSwitch_Change?(obj.isOn)
    }

    //MARK:-  button clicked
    @IBAction func btnInfo_Clicked(_ sender: UIButton) {
        self.blockBtn_Clicked?()
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Uses of this cell

let cell = tableView.dequeueReusableCell(withIdentifier: CellSwitch.identifier) as? CellSwitch
                cell?.lblTitle.textColor = Color.custom(hexString: objModel.titleLblColor, alpha: 1.0).value
                cell?.lblTitle.text = objModel.strTitle
                cell?.objSwitch.isOn = objModel.isOn
                cell?.btnInfo.isHidden = !objModel.isInfoBtn
                cell?.blockBtn_Clicked = { 
                   print("button clicked")
                }
                cell?.blockSwitch_Change = { (isOn) in
                    print("switch value changed \(isOn)")
                }
                if objModel.isEnable == false
                {
                    cell?.isUserInteractionEnabled = false
                    cell?.contentView.alpha = 0.5
                }
                else
                {
                    cell?.isUserInteractionEnabled = true
                    cell?.contentView.alpha = 1.0
                }
                return cell!

Also if i comment this two blocks then my deinit will called.

4

1 回答 1

3

听起来您正在通过在块内强烈引用视图控制器来创建保留周期。相反,您应该创建对要使用的 vc 的弱引用。这是我的首选方法

cell?.blockBtn_Clicked = { [weak self]
    print("button clicked")
    self?.viewModel.//do something
}
cell?.blockSwitch_Change = { [weak self] (isOn) in
    print("switch value changed \(isOn)")
    self?.viewModel.//do something
}

[weak self] 部分会将 self 的弱引用传递到块中,尽管请注意此引用现在是可选的。然后,您可以使用可选链接或在此之后解开它。

于 2018-07-10T11:08:14.250 回答