UITableView 的 headerview 中的 UISwitch 正在更改状态但不调用事件处理方法

UISwitch in headerview of UITableView is changing state but not calling event handle method

我正在开发一个项目,该项目在 header 视图上有 UITableViewUISwitch。以下是我的 header 视图代码。我在 headerview 上尝试了有关按钮单击事件的不同问题,但 none 的解决方案似乎有效。

如果有帮助,我正在设置 UITableView header 高度。

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 100;
}

这是我的 header.

的代码
class NotificationSettingsTableHeaderView : UIView, ViewProtocol {

    //other controls
    var mainSwitch : UISwitch!
    var contentView : UIView!

    weak var temp: SettingCellDelegate?

    func handle(sender: UISwitch) {

     // ----------- THIS METHOD IS NOT GETTING CALLED ------------
        if(sender.isOn){
            print("IS ON")
        }
        else{
            print("IS OFF")
        }

    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubviews()
        makeConstraints()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func addSubviews() {

        contentView = UIView()

    // Other control initialization
        mainSwitch = UISwitch()
        mainSwitch.addTarget(self, action: #selector(NotificationSettingsTableHeaderView.handle(sender:)), for: UIControlEvents.valueChanged)
        contentView.addSubview(mainSwitch)

    }

    func makeConstraints() {

        let screen = UIScreen.main.bounds

        contentView.frame = CGRect(x: 0, y: 0, width: screen.width, height: 80)
        mainSwitch.snp.makeConstraints { (make) in
            make.centerY.equalTo(contentView)
            make.right.equalTo(contentView).offset(-10)
        }
        // other code

    }    
}

已编辑

我正在使用以下代码将内容视图添加到 header。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {

    let cell = NotificationSettingsTableHeaderView()
    let cat = preferenceSection.allSections[section]
    cell.label.text = cat.getTitle()
    cell.descLabel.text = cat.getSubtitle()
    return cell.contentView
}

这是我的 header 视图的样子。我什至可以转动 on/off 开关

检查你的 UISwitch 是否在你的 UIView 层次结构中,正如@DonMag 所说,你似乎忘记了将你的 contentView 添加到视图层次结构,你的 handle 方法是一个实例方法,不要 class 方法,所以

在您的 addSubviews 方法中更改此行

mainSwitch.addTarget(self, action: #selector(NotificationSettingsTableHeaderView.handle(sender:)), for: UIControlEvents.valueChanged)

靠这个

mainSwitch.addTarget(self, action: #selector(self.handle(sender:)), for: UIControlEvents.valueChanged)

已更新 1

您需要 return 您的 NotificationSettingsTableHeaderView 对象,而您正在 return 使用 contentView

UITableViewDelegate

viewForHeaderInSection 方法中更改此行
return cell.contentView

由这个

return cell

已更新 2

func addSubviews() {

        contentView = UIView()

    // Other control initialization
        mainSwitch = UISwitch()
        mainSwitch.addTarget(self, action: #selector(NotificationSettingsTableHeaderView.handle(sender:)), for: UIControlEvents.valueChanged)
        self.addSubview(mainSwitch)
    }

func makeConstraints() {

    mainSwitch.snp.makeConstraints { (make) in
        make.centerY.equalTo(self)
        make.right.equalTo(self).offset(-10)
    }
    // other code

}