I am working on a project which has UITableView
with UISwitch
on headerview. Following is my code for header view. I have tried different questions regarding button click event on headerview but none of the solutions seems to work.
If it helps, I am setting UITableView
header height.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100;
}
Here is the code for my 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
}
}
Edited
I am adding contentview to header using following code.
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
}
Here is how my header view look. I am even able to turn on/off switch
Check that you have your UISwitch in your
UIView
hierarchy, as @DonMag says, seems that you have forgot add your contentView to view hierarchy, Your handle method is an instance method, don't a class method, soChange this line in your
addSubviews
methodby this one
Updated 1
You need return your
NotificationSettingsTableHeaderView
object and you are returning the contentView insteadchange this line in your
viewForHeaderInSection
method of yourUITableViewDelegate
by this one
Updated 2