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
}
}
In Swift 4 you can add a UITapGestureRecognizer to the header view:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(headerTap))
headerView.addGestureRecognizer(tapRecognizer)
return headerView
}
@objc func headerTap() {
// Tap action
}
I did it this way.
First, make a UITableViewCell class with a "headerCellSection" variable (or whatever you want to call it for that matter).
import UIKit
class HeaderCellTableViewCell: UITableViewCell {
var headerCellSection:Int?
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Add the cell to "viewForHeaderInSection":
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! HeaderCellTableViewCell
return headerCell
}
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:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! HeaderCellTableViewCell
// Send section
headerCell.headerCellSection = section
// Add gesture
let headerTapGesture = UITapGestureRecognizer()
headerTapGesture.addTarget(self, action: "myAction:")
headerCell.addGestureRecognizer(headerTapGesture)
return headerCell
}
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!
func myAction (sender: UITapGestureRecognizer) {
// Get the view
let senderView = sender.view as! HeaderCellTableViewCell
// Get the section
let section = senderView.headerCellSection
print(section)
}
I did it by adding the UITapGestureRecognizer
to the contentView
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.
let vvv = tableView.headerViewForSection(section)
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.
tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: sectionHeaderIdentifier)
Above registration, I did in my viewDidLoad() function. Then use below code
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView: UITableViewHeaderFooterView = tableView.dequeueReusableHeaderFooterView(withIdentifier: sectionHeaderIdentifier)!
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(sectionHeaderTapped))
sectionView.addGestureRecognizer(tapGesture)
return sectionView
}
Above function will create the default view for your table section headers with the tap gesture attribute enabled.