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
Simple drop in solution for Swift 3.0
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UITableViewHeaderFooterView()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
v.addGestureRecognizer(tapRecognizer)
return v
}
func handleTap(gestureRecognizer: UIGestureRecognizer)
{
controller?.view.endEditing(true)
}
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")
}
My implementation in Swift:
In your UITableViewController
:
let myView = MyView()
let tapRecognizer = UITapGestureRecognizer(target: myView, action: "handleTap")
myView.addGestureRecognizer(tapRecognizer)
self.tableView.tableHeaderView = myView
In MyView
:
class MyView: UIView {
func handleTap() {
// Handle touch event here
}
}
Create a custom section headerView.. and then add Tap gesture to it.
UITapGestureRecognizer * recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
//This method will be called on Tap of section header
- (void)handleTap:(UITapGestureRecognizer *)sender {
//Do the action on Tap
}
If you need to know the index of the tapped section, you can use the following pattern (Swift 4) :
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 44))
// Configure your view here
// ...
let tapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(headerTapped(_:))
)
view.tag = section
view.addGestureRecognizer(tapGestureRecognizer)
return view
}
@objc func headerTapped(_ sender: UITapGestureRecognizer?) {
guard let section = sender?.view?.tag else { return }
// Use your section index here
// ...
}
swift solution for this:
let tapR : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "gotoHeaderArticle")
tapR.delegate = self
tapR.numberOfTapsRequired = 1
tapR.numberOfTouchesRequired = 1
yourView.addGestureRecognizer(tapR)
Then implement
func gotoHeaderArticle() {
}
Make sure to make your calling viewcontroller adhere to UIGestureRecognizerDelegate