Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make UITableView Header selectable?

Tags:

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?

like image 465
kaymas Avatar asked Nov 15 '13 08:11

kaymas


People also ask

How do I scroll the header along with UITableView?

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.

What is Section in UITableView?

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.

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


2 Answers

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.

like image 145
Dhaval Bhadania Avatar answered Jan 01 '23 18:01

Dhaval Bhadania


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     // ... } 
like image 29
dulgan Avatar answered Jan 01 '23 16:01

dulgan