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 I can't interact with its controls.
I need to know how can I make this header view selectable?
If you have a single header in the table then you can use tableHeaderView as below: tableView. tableHeaderView = Header; Or if you have multiple header in table than you need to use Group table instead of plain table.
UITableView with sections allows us to separate the list into different categories, so the list looks more organized and readable. We can customize sections as per our need, but in this tutorial, we are covering the basic UITableview with sections. Here's is the video if you prefer video over text. Let Create An App.
A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+
There is no way to do it with the UITableViewDelegate.
You have to add a UITapGestureRecognizer to yourHeaderView Like:
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; [singleTapRecognizer setDelegate:self]; singleTapRecognizer.numberOfTouchesRequired = 1; singleTapRecognizer.numberOfTapsRequired = 1; [yourHeaderView addGestureRecognizer:singleTapRecognizer]; -(void) handleGesture:(UIGestureRecognizer *)gestureRecognizer; //do in this method whatever you want
FOR Swift 3.0
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(YOURVIEWCONTROLLER.TapGestureRecognizer(_:))) yourHeaderView.addGestureRecognizer(tapGesture) func TapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) { //do your stuff here }
Swift >= 4.0
Add UIGestureRecognizerDelegate class reference inherited
let tap = UITapGestureRecognizer(target: self, action:#selector(self.handleTap(_:))) tap.delegate = self self.view.addGestureRecognizer(tap) @objc func handleTap(_ sender: UITapGestureRecognizer) { //Do your work }
may be it will help.
Happy Coding.
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 // ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With