【发布时间】:2018-02-03 14:29:06
【问题描述】:
我正在开发一个在 headerview 上有 UITableView 和 UISwitch 的项目。以下是我的标题视图代码。我尝试了有关 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