【问题标题】:UISwitch in headerview of UITableView is changing state but not calling event handle methodUITableView 的 headerview 中的 UISwitch 正在更改状态但未调用事件句柄方法
【发布时间】:2018-02-03 14:29:06
【问题描述】:

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

如果有帮助,我将设置 UITableView 标题高度。

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

这是我的标题的代码。

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

    }    
}

已编辑

我正在使用以下代码将 contentview 添加到标题中。

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
}

这是我的标题视图的外观。我什至可以打开/关闭开关

【问题讨论】:

  • 您是否在标题视图中看到 UISwitch?您在标题视图中看到 anything 了吗?我看不到您将 contentView 添加到标题视图的位置...
  • 是的,我可以看到整个标题视图。我什至可以点击UISwitch 并打开/关闭它。 @DonMag
  • @DonMag 请查看我编辑的问题。

标签: ios uitableview swift3 uiswitch


【解决方案1】:

检查您的UIView 层次结构中是否有 UISwitch,正如@DonMag 所说,您似乎忘记将 contentView 添加到视图层次结构中,您的句柄方法是实例方法,而不是类方法,所以

addSubviews 方法中更改这一行

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

这个人

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

更新 1

您需要返回您的 NotificationSettingsTableHeaderView 对象,而您正在返回 contentView

UITableViewDelegateviewForHeaderInSection 方法中更改此行

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

}   

【讨论】:

  • 我也试过了。它仍然无法正常工作。 @Reinier Melian
  • @Pooja 再次检查我的答案
  • @Pooja 您需要在部分方法中的 viewForHeader 中返回单元格,您正在返回内容视图,这就是为什么根本不起作用
  • 如果我返回单元格,那么我将无法看到我的整个标题。 @Reinier Melian
  • @Pooja - 在NotificationSettingsTableHeaderView里面,需要添加contentView作为子视图,然后使用整个视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
相关资源
最近更新 更多