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)
}