2016-02-07 167 views
0

我有一個UILabel一個UITapGestureRecognizerUITableViewCell,我添加所述UITapGestureRecognizer的目標作爲視圖控制器,如:的UITableViewCell:UITapGestureRecognizer無法識別的選擇發送到實例錯誤

class Cell: UITableViewCell { 

    @IBOutlet weak var label: UILabel! 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     let tap1 = UITapGestureRecognizer(target: ViewController(), action: "selectLabel:") 
     label.addGestureRecognizer(tap) 
    } 
} 

class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate{ 

    fun selectLabel(gesture: UITapGestureRecognizer){ 
     //animate some view in custom cell 
    } 
} 

然而,我得到一個「UITapGestureRecognizer無法識別的選擇器發送到實例」錯誤。

+1

設置的手勢,你能告訴我們什麼 「視圖控制器()」 代表? :) – Eendje

回答

0

cellForRowAtIndexPath.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as! Cell 
    let tap1 = UITapGestureRecognizer(target: self, action: "selectLabel:") 
    cell.label.addGestureRecognizer(tap1) 
    return cell 
    } 
+0

這樣做會創建一個強大的引用循環,除非您使用':gestureRecognizers?.forEach(removeGestureRecognizer)'在例如'prepareForReuse'中刪除手勢。 – Eendje

相關問題