Swift3.0/iOS11.3 の環境です。
UITableViewで簡単なリストを作成しました。
テーブルを構成するcell(UITableViewCell)は、一つのUIImageViewを持っているだけのシンプルな作りです。
ImageViewには、適当に♡マークの画像を貼り付けました。
TableViewCellのクラスでは、自身のImageViewをタップされたときに、ログを吐き出すようにしてあります。
Swift
1import UIKit 2 3class TableViewCell: UITableViewCell { 4 5 @IBOutlet weak var iconImageView: UIImageView! 6 7 override func awakeFromNib() { 8 super.awakeFromNib() 9 self.iconImageView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(self.onTapIcon(_:)))) 10 11 } 12 13 override func setSelected(_ selected: Bool, animated: Bool) { 14 super.setSelected(selected, animated: animated) 15 } 16 17 @objc func onTapIcon(_ sender : UITapGestureRecognizer) { 18 print("アイコンがタップされました!!") 19 } 20}
ですが、ハートのアイコンをいくらタップしても、onTapIcon()が呼ばれることがありません。
なぜでしょうか?
ちなみに、TableViewを保有しているViewControllerは以下のようなコードになっています。
Swift
1import UIKit 2 3class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 4 5 @IBOutlet weak var tableView: UITableView! 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 // Do any additional setup after loading the view. 11 self.tableView.dataSource = self 12 self.tableView.delegate = self 13 } 14 15 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 16 17 return 100 18 } 19 20 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 21 return 10 22 } 23 24 func numberOfSections(in tableView: UITableView) -> Int { 25 return 1 26 } 27 28 29 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 30 31 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell 32 33 cell.iconImageView.image = #imageLiteral(resourceName: "heart") 34 return cell 35 36 } 37 38 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 39 print("(indexPath.row)番目のセルがタップされました。") 40 } 41}
ハートマークをタップすると、tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) のイベントが実行されて、**「X番目のセルがタップされました。」**のみが出力されます。
どなたかアドバイスいただけるでしょうか?
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/31 03:01