2016-04-11 6 views
0

私はUITableViewを持っており、UILongPressGestureRecognizerを実装しました。セルを長押しすると、handleLongPressという関数が呼び出されます。AnyObjectセルのタイプをチェックする方法は?

func handleLongPress(sender:AnyObject){ 

} 

問題があり、UITableViewはので、私は長いクリックするcellの種類を知っておく必要があり、私の中UITableViewCellsにはいくつかの種類があります。私のカスタムセルの例:

if sender is ProfileTableViewCell{ 
      print("Long Clicked!") 
     } 

そして、この:これらの作業の

if let mType = sender as? ProfileTableViewCell{ 
      print("Long Clicked!") 
     } 

なし

import UIKit 

class ProfileTableViewCell: UITableViewCell { 

    @IBOutlet weak var ivProfile: UIImageView! 
    @IBOutlet weak var tvName: UILabel! 
    @IBOutlet weak var tvEmail: UILabel! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 

は、私はこれを試してみました。どのように細胞のタイプをチェックするのですか?

編集:これは、ジェスチャー認識を割り当てる方法です:

let cell = tableView.dequeueReusableCellWithIdentifier("profileCell", forIndexPath: indexPath) as! ProfileTableViewCell 

longPress = UILongPressGestureRecognizer(target: self, action: #selector(ProfileTableViewController.handleLongPress(_:))) 
longPress.minimumPressDuration = 0.5 
longPress.delaysTouchesBegan = true 
longPress.delegate = self 
cell.addGestureRecognizer(longPress) 
+0

どのようにジェスチャー認識を割り当てていますか? – xoudini

+0

私は編集として追加しました。 –

+0

さて、あなたが 'UITableViewCell'のサブクラスだけを送信者にすることを期待しているならば、' sender:AnyObject'を 'sender:UITableViewCell'に変更することができます。 – xoudini

答えて

1

だから、他のすべては、ちょうどのようなハンドラを変更、罰金だ:

func handleLongPress(sender: UIGestureRecognizer) { 
    switch sender.view { 
    case is ProfileTableViewCell: print("Is a ProfileTableViewCell") 
    default: print("Is not.") 
    } 
} 
+0

これは動作するようです。時にはそれは長いプレスを検出しませんが、私はそれを調べます。ありがとう! –

関連する問題