<Swift>UITableViewHeaderFooterViewのlabelにUITapGestureRecognizerを設定しても動作しない
UITableViewHeaderFooterViewで設定する場合
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")
}
}
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")
}
}
★補足
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で設定する場合
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)")
}
}
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)")
}
}
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)")
}
}
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)")
}
}
Author And Source
この問題について(<Swift>UITableViewHeaderFooterViewのlabelにUITapGestureRecognizerを設定しても動作しない), 我々は、より多くの情報をここで見つけました https://qiita.com/mas821/items/6311d12e8e11d54835ab著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .