2016-10-10 11 views
0

現在、3つのセルを持つTableViewControllerがあり、検出されたときにログに印刷するために長いプレスジェスチャー認識子を追加しようとしています。テーブルビューのセルにジェスチャーレコーダーを追加する

私が追加しました:

class TableTesting: UITableViewController, UIGestureRecognizerDelegate 

との私のtableView方法で、私が作成したUILongPressGestureRecognizer

func longPressAction(gestureRecognizer: UILongPressGestureRecognizer) { 
    print("Gesture recognized") 
} 
:私も機能 longPressActionを作成しました
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
    cell.textLabel?.text = "Gesture Recognizer Testing" 
    var lpgr = UILongPressGestureRecognizer(target: self, action: "longPressAction:") 
    lpgr.minimumPressDuration = 2.0 
    lpgr.delegate = self 
    cell.addGestureRecognizer(lpgr) 
    return cell 
} 

私が抱えている問題は、コードをコンパイルして長いpをしようとするときですRESS 1私の細胞は、アプリがクラッシュすると、私はこのエラーを取得しています:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestingGround.TableTesting longPressAction:]: unrecognized selector sent to instance 0x7f9afbc055d0'

を私は正しい情報が関数に渡されていませんが、私はわからないだと何とか推測していますか?

ご協力いただければ幸いです。

+0

'' longPressAction: "'の代わりに、 '#selector(longPressAction(_ :))'表記を使用してください。 '#selector'を使うとオートコンプリート機能が得られますし、何か正しいことをせず、あなたが望む方法を見つけることができない場合にも警告します。 – keithbhunter

+0

また、ジェスチャ認識機能を 'cell'の代わりに' cell.contentView'に追加してください。 – AdamPro13

+0

だから私は持っているものの代わりに、私は使用する必要があります: 'let lpgr = UILongPressGestureRecognizer(target:self、action:#selector(longPressAction(_ :))' –

答えて

0

事はあなたが道の正しいほとんどを行っているです。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
    cell.textLabel?.text = "Gesture Recognizer Testing" 
    let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(_:))) 
    lpgr.minimumPressDuration = 2.0 
    lpgr.delegate = self 
    cell.contentView.addGestureRecognizer(lpgr) 
    return cell 
} 

乾杯: は、このコードを使用してください!

+1

ありがとう、たくさん助けました:)それは働いています、今はちょうどそれが初めて印刷することを確認するためにいくつかのチェックを行う必要があります。 –

+0

乾杯!メイト、upvoteし、受け入れられた答えとしてマーク:) :) –

+0

私は私の機能内の細胞に触れることを知っていますか?だから私は 'セル1ロングプレス検出'のようなものを印刷することができますか? –

1

の代わりに:

var lpgr = UILongPressGestureRecognizer(target: self, action: "longPressAction:") 

用途:

let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(gestureRecognizer:))) 
+0

ありがとう、それは今働いている! :) –

関連する問題