LoginSignup
20
19

More than 5 years have passed since last update.

UITapGestureRecognizerのせいでUITableViewに対するタップの処理が実行されない問題の解決策

Last updated at Posted at 2015-10-09
//  var myTableView: UITableView!
self.view.addSubview(myTableView)

の状態で

let myTap = UITapGestureRecognizer(target: self, action: "tapGesture:")
self.view.addGestureRecognizer(myTap)

とすると、addGesture無しであれば呼び出されるはずの

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("Num: \(indexPath.row)")
        print("Value: \(settingItems[indexPath.row])")
        // Access the image or the cell at this index path
    }

このメソッドが呼び出されない。

仕方がないので、tapGestureの方でtableViewがタップされたことを認識する処理を書く

internal func tapGesture(sender: UITapGestureRecognizer){
        let touch = sender.locationInView(settingTableView)
        if let indexPath = settingTableView.indexPathForRowAtPoint(touch) {
            print("Num: \(indexPath.row)")
            print("Value: \(settingItems[indexPath.row])")
            // Access the image or the cell at this index path
        }
    }

こんな感じ。

サンプルコードはこちら。

import UIKit

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  private var settingTableView: UITableView!
    override func viewDidLoad() {
        initTableView()
        let myTap = UITapGestureRecognizer(target: self, action: "tapGesture:")
        self.view.addGestureRecognizer(myTap)
    }

    internal func tapGesture(sender: UITapGestureRecognizer){
        //スクロールビューの影響でテーブルビューのタップが認識されないことへの対策
        let touch = sender.locationInView(settingTableView)
        if let indexPath = settingTableView.indexPathForRowAtPoint(touch) {
            print("Num: \(indexPath.row)")
            print("Value: \(settingItems[indexPath.row])")
            // Access the image or the cell at this index path
        }
    }

    func initTableView() -> UITableView {


        // Viewの高さと幅を取得する.
        let displayWidth: CGFloat = self.view.frame.width
        let displayHeight: CGFloat = self.view.frame.height

        // TableViewの生成する
        myTableView = UITableView(frame: CGRect(x: 0, y: profileViewHeight, width: displayWidth, height: displayHeight - profileViewHeight))

        // Cell名の登録をおこなう.
        myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell")

        // Cellの高さを指定する
        myTableView.rowHeight = 60

        //セパレーターの色を変える
        myTableView.separatorColor = UIColor.lightGrayColor()

        // DataSourceの設定をする.
        myTableView.dataSource = self

        // Delegateを設定する.
        myTableView.delegate = self

        return myTableView
    }

    /*
    Cellが選択された際に呼び出されるデリゲートメソッド.
    */
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //print("Num: \(indexPath.row)")
        //print("Value: \(settingItems[indexPath.row])")
        //touchRecognizerのせいで呼び出されません!!
    }

    /*
    Cellの総数を返すデータソースメソッド.
    (実装必須)
    */
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return settingItems.count
    }

    /*
    Cellに値を設定するデータソースメソッド.
    (実装必須)
    */
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        // 再利用するCellを取得する.
        let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath)
        // Cellに値を設定する.
        cell.textLabel!.text = "\(settingItems[indexPath.row])"
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell.imageView!.image = UIImage(named: "sample")
        cell.textLabel!.font = UIFont(name: "Arial", size: 22)
        cell.textLabel?.textColor = UIColor.orangeColor()

        return cell
    }

}


tableViewのサンプルは以下のサイトを参考にしました
https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/uikit/006-uitableviewdeteburuwo-biao-shi

回避策についてはstackoverflowを参考にしました
http://stackoverflow.com/questions/29360673/getting-uitableviewcell-from-uitapgesturerecognizer

あんまりきれいな解決策ではないのかもしれないのでより良い方法があれば教えて下さい。
xcode 7.0.1
swift 2.0

20
19
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
19