代码之家  ›  专栏  ›  技术社区  ›  Chirag Shah

在Swift 3.0中使用的块中未调用DEinit

  •  1
  • Chirag Shah  · 技术社区  · 6 年前

    我在单元中使用了块来获取开关值,但现在我的问题是,在使用块的地方没有调用deinit。它以前完全可以工作,但在Swift 3.0中不工作。

    我的交换单元:

    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
        }
    
    }
    

    此单元格的用途

    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!
    

    另外,如果我评论这两个街区,我的神就会打电话给我。

    1 回复  |  直到 6 年前
        1
  •  3
  •   Aidan Malone    6 年前

    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
    }