I have made a custom section-header for UITableView, that includes some controls like segmented control, UIImageView ,etc. It successfully appears, but it\'s not tappable so
If anyone needs a solution that can work when the tableview has multiple sections, I had the same problem and the solution I came up with was to create different targets on a button at the viewForHeaderInSection method, based on the section. I am using a button but this should work just as well with UITapGestureRecognizer
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// ...
if section == 0 {
cell.button.addTarget(self, action: #selector(firstSectionTapped(_:)), for: .touchUpInside)
} else if section == 1 {
cell.button.addTarget(self, action: #selector(secondSectionTapped(_:)), for: .touchUpInside)
}
// ...
}
Then in the ViewController:
@objc func goalCategoryTapped(_ sender: UITapGestureRecognizer?) {
print("Section One Tapped")
}
@objc func ideaCategoryTapped(_ sender: UITapGestureRecognizer?) {
print("Section Two Tapped")
}