＜Swift＞UITableViewHeaderFooterViewのlabelにUITapGestureRecognizerを設定しても動作しない


# UITableViewHeaderFooterViewで設定する場合


```swift:TopTableSectionHeaderView.swift
import UIKit
import RxSwift

class TopTableSectionHeaderView: UITableViewHeaderFooterView {

    @IBOutlet weak var categoryNameLabel: UILabel!
    
    @IBOutlet weak var categoryAddButton: UIButton!

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        commonInit()
    }
    
    func commonInit(){
        let _view = UINib(nibName: "TopTableSectionHeaderView", bundle: nil)
            .instantiate(withOwner: self, options: nil)
            .first as! UIView
        
        _view.frame = bounds
        _view.backgroundColor = .green
        addSubview(_view)
        _view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        
        // うまくいく(printが実行される)
        let categoryAddButtonObservable = categoryAddButton.rx.tap.asObservable()
        categoryAddButtonObservable.subscribe(
            onNext: { [weak self] in
            print("rxButton")
            }
        )

        // うまくいかない(printが実行されない)
        let tapGesture = UITapGestureRecognizer()
        tapGesture.rx.event.bind(onNext: { recognizer in
            print("rxLabel")
        }).disposed(by: DisposeBag())
        _view.addGestureRecognizer(tapGesture)

        // うまくいかない(printが実行されない)
        let tapLabelGesture = UITapGestureRecognizer(target: self, action: #selector(labelDidTap(_:)))
        categoryNameLabel.addGestureRecognizer(tapLabelGesture)
    }

    @objc func labelDidTap(_ sender: UITapGestureRecognizer) {
        print("label")
    }
}
```

★補足

```swift:TopTableSectionHeaderView.swift
import UIKit

class TopTableSectionHeaderView: UITableViewHeaderFooterView {

    @IBOutlet weak var categoryNameLabel: UILabel!

    @IBOutlet weak var categoryAddButton: UIButton!
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        commonInit()
    }
    
    func commonInit(){
        let _view = UINib(nibName: "TopTableSectionHeaderView", bundle: nil)
            .instantiate(withOwner: self, options: nil)
            .first as! UIView
        
        _view.frame = bounds
        _view.backgroundColor = .green
        addSubview(_view)
        _view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        
        // うまくいく(printが実行される)
        let tapViewGesture = UITapGestureRecognizer(target: self, action: #selector(viewDidTap(_:)))
        _view.addGestureRecognizer(tapViewGesture)
        
    }
    
    @objc func viewDidTap(_ sender: UITapGestureRecognizer) {
        print("view")
    }
}
```

# ViewControlloerで設定する場合

```swift:TopViewController.swift
extension TopViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "TopTableSectionHeaderView")
        if let v = view as? TopTableSectionHeaderView {
            v.setText("cate header!!!")
        }

        // うまくいく(printが実行される)
        let gesture = UITapGestureRecognizer(target: self, action: #selector(sectionHeaderDidTap(_:)))
        view?.addGestureRecognizer(gesture)
        view?.tag = section
        
        // うまくいく(printが実行される)
        let tapGesture = UITapGestureRecognizer()
        view?.addGestureRecognizer(tapGesture)
        tapGesture.rx.event.bind(onNext: { recognizer in
            print("rx")
        }).disposed(by: disposeBag)
                
        return view
    }
    
    @objc func sectionHeaderDidTap(_ sender: UITapGestureRecognizer) {
        print("tag \(sender.view?.tag)")
    }
}
```


```swift:TopViewController.swift
extension TopViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "TopTableSectionHeaderView")
        if let v = view as? TopTableSectionHeaderView {
            v.setText("cate header!!!")
                        
            // うまくいかない(printが実行されない)
            let tapGesture = UITapGestureRecognizer()
            v.categoryNameLabel.addGestureRecognizer(tapGesture)
            tapGesture.rx.event.bind(onNext: { recognizer in
                print("rx")
            }).disposed(by: disposeBag)
            
            // うまくいかない(printが実行されない)
            let gesture = UITapGestureRecognizer(target: self, action: #selector(sectionHeaderLabelDidTap(_:)))
            v.categoryNameLabel.addGestureRecognizer(gesture)
            v.categoryNameLabel.tag = section   
        }
        return view
    }
    
    @objc func sectionHeaderLabelDidTap(_ sender: UITapGestureRecognizer) {
        print("tag \(sender.view?.tag)")
    }
}
```