I trying to use expandable tableview in swift. So I added Gesture to header in tableview, but it does not recognising. Kindly guide me. Just cross check my coding.
MY CODING IS BELOW:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let vvv = tableView.headerViewForSection(section)
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
vvv?.addGestureRecognizer(tap)
return vvv
}
func handleTap(gestureRecognizer: UITapGestureRecognizer)
{
if(gestureRecognizer.state == .Ended)
{
println("SECTION PRESSED") //NOT ENTERING TO THIS BLOCK
}
}
I did it this way.
First, make a UITableViewCell class with a "headerCellSection" variable (or whatever you want to call it for that matter).
Add the cell to "viewForHeaderInSection":
Send the cell's section to the previously declared variable and add the tap gesture recogniser to the cell. The code should look something like this:
Then add the action. In the action you can access the view that has the tap gesture target. There you can access tour "headerCellSection" value and voila!
In Swift 4 you can add a UITapGestureRecognizer to the header view:
I did it by adding the
UITapGestureRecognizer
to thecontentView
of the headerView. Example:headerCell.contentView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(headerTapped)))
I know this is a bit old question to give an answer. But none of the answers accepted, I will give another simple solution.
After I read original question, I thought the author do not want a custom created section header. That's why he was trying to use following code.
But I'm sure you will get a null value for vvv in the above code snippet. If user wants to access the default view, he can do it as below.
Above registration, I did in my viewDidLoad() function. Then use below code
Above function will create the default view for your table section headers with the tap gesture attribute enabled.