Je ne sais pas si je t’apporte une solution qui conviendra, mais voila ce que j’ai fait.
La fonction de base pour savoir si on a cliqué sur une ligne est celle-ci :
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
print(indexPath)
return indexPath
}
Effectivement, quand on clic sur une cellule dite “normale”, on a un resultat du type
[0,1]
Et quand on clic sur une entete de section, on a un truc similaire.
L’idée est de faire un gestionnaire de clic a la main.
Dans ta méthode qui gère la cellule d’entete tu fait comme ca :
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// tap
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleHeaderTap))
tapGestureRecognizer.delegate = self
tapGestureRecognizer.numberOfTapsRequired = 1
tapGestureRecognizer.numberOfTouchesRequired = 1
// cell
let headerCell = tableView.dequeueReusableCell(withIdentifier: "header") as! CustomHeaderCell
headerCell.headerLabel?.text = continents[section].name
headerCell.addGestureRecognizer(tapGestureRecognizer)
return headerCell
}
Et plus loin dans le code tu as ta méthode a ajouter qui va gérer le clic :
@objc func handleHeaderTap(gestureRecognizer: UIGestureRecognizer)
{
print("Tapped")
}
Très important, ne pas oublier d’implémenter la classe UIGestureRecognizerDelegate
pour le delegate
Le code final fonctionnel :
import UIKit
class Continent {
var name: String;
init(name: String) {
self.name = name;
}
}
class Country {
var continent: Continent;
var name: String;
init(name: String, continent: Continent) {
self.continent = continent;
self.name = name;
}
}
class TableViewController: UITableViewController, UIGestureRecognizerDelegate {
var continents: Array<Continent> = []
var countries: Array<Country> = []
override func viewDidLoad() {
super.viewDidLoad()
continents.append(Continent(name: "Europe"))
continents.append(Continent(name: "Asie"))
continents.append(Continent(name: "Amérique du sud"))
countries.append(Country(name: "France", continent: continents[0]))
countries.append(Country(name: "Espagne", continent: continents[0]))
countries.append(Country(name: "Allemagne", continent: continents[0]))
countries.append(Country(name: "Japon", continent: continents[1]))
countries.append(Country(name: "Chine", continent: continents[1]))
countries.append(Country(name: "Inde", continent: continents[1]))
countries.append(Country(name: "Corée", continent: continents[1]))
countries.append(Country(name: "Argentine", continent: continents[2]))
countries.append(Country(name: "Brésil", continent: continents[2]))
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return continents.count;
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countries.filter({ (country:Country) -> Bool in
return country.continent === continents[section]
}).count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = countries[indexPath.row].name
return cell
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// tap
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleHeaderTap))
tapGestureRecognizer.delegate = self
tapGestureRecognizer.numberOfTapsRequired = 1
tapGestureRecognizer.numberOfTouchesRequired = 1
// cell
let headerCell = tableView.dequeueReusableCell(withIdentifier: "header") as! CustomHeaderCell
headerCell.headerLabel?.text = continents[section].name
headerCell.addGestureRecognizer(tapGestureRecognizer)
return headerCell
}
//test 1
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
print(indexPath)
return indexPath
}
//test 2
@objc func handleHeaderTap(gestureRecognizer: UIGestureRecognizer)
{
print("Tapped")
}
}