2017-10-19 136 views
0

我需要通過點擊一個tableViewCell, 觸發一個函數,直到現在我使用@IBAction,但該選項僅適用於按鈕類型(我還沒有找到另一種方式..) this就是這樣,我現在的:swift @IBAction tableViewCell

@IBAction func springPrs(_ sender: Any) { 
    //doing stuff.. 
} 

,但現在我有一個@IBOutlet

@IBOutlet weak var nextTrackCell: nextTableViewCell! 

,我想通過點擊觸發功能。任何幫助?

回答

1

這是一種錯誤的做法,你應該實現從UITableViewDelegate委託方法稱爲didSelectRowAt

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
      //do your stuff here. 
     } 
+0

感謝,但事情是我創建了一個表視圖之外的燎細胞,我在新的迅疾,我猜這不是做正確的事(我想知道的缺點在這方面),但仍然有辦法嗎? – hhh

+0

是的,這是做你想做的事的正確方法。另外,你不應該在tableview之外創建一個單元格。單元格是爲使用tableview而製作的,而不是單獨使用。如果您使用的是單個單元格,請考慮將其切換爲UIView而不是 –

+0

,以及如何爲UIview創建「onclick」函數? – hhh

0

不應該直接添加動作到表格視圖單元格,因爲它違反了MVC設計模式,有是一個方便的回調已經內置到UITableViewDelegate,使這真的很容易。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if indexPath.row == 0 { 
     // do something when the top row tapped 
    } else { 
     // do something when any other row is tapped 
    } 
} 
+0

我認爲override關鍵字會觸發編譯器錯誤。 –

+0

@LuizFernandoSalvaterra你是對的,我的道歉。如果使用已經實現委託回調的'UITableViewController',那將是代碼。 –

+0

是的,我也使用這種方法以及哈哈哈 –