2017-08-08 59 views
0

我試圖按下tableview上的按鈕時執行某些操作。我添加的按鈕以在故事板的單元以及連接它這樣:按鈕目標不會執行TableViewCell中的函數

@IBOutlet weak var likeMessage: UIButton! 

然後,我添加了一個目標和在所述cellForRowAt的按鈕標籤。

cell.likeMessage.tag = indexPath.row 
    cell.likeMessage.addTarget(self, action: #selector(SignalRViewController.handleLikes(sender:)), for:.touchUpInside) 

而且我在同一個類(SignalRViewController)中有handleLikes函數。

func handleLikes(sender: UIButton){ 
     print("was clicked") 
    } 

我發現其他職位這一方式,但 我無法得到它的工作。我錯過了什麼?

更新

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     self.tableViewMessage.backgroundView = nil 
      //My Messages 
     guard let cell = tableView.dequeueReusableCell(withIdentifier: "myMessageCell", for: indexPath) as? MyMessageTableViewCell else { 
      fatalError("The dequeued cell is not an instance of Community") 
      } 
      cell.imgUser.layer.cornerRadius = cell.imgUser.frame.height/2 
      cell.imgUser.clipsToBounds = true 
      cell.imgUser.layer.borderWidth = 3.0 
      cell.lblMessage.lineBreakMode = .byWordWrapping // or NSLineBreakMode.ByWordWrapping 
      cell.lblMessage.numberOfLines = 0 
      cell.background.layer.cornerRadius = 7 
      cell.lblMessage.text = "\(arrayMessage[indexPath.row][0])" 
      cell.lblMessage.textAlignment = .left 

      cell.imgUser.layer.borderColor = UIColor(red:222/255.0, green:225/255.0, blue:227/255.0, alpha: 1.0).cgColor 
      cell.background.backgroundColor = constants.duckEggBlue 
      cell.imgUser.downloadedFrom(link: userImageServer) 
      cell.lblTimestamp.text = getFormattedTime(index: indexPath.row, cell: "myCell") 
      cell.likeMessage.tag = indexPath.row 
      cell.likeMessage.addTarget(self, action: #selector(handleLikes(sender:)), for:.touchUpInside) 

      return cell} 

Picture with the Storyboard

+1

請寫上UITableViewCell類,方法handleLikes。 – shahnilay86

+0

嘗試使用它,因爲它是在同一個類中: 'cell.likeMessage.addTarget(self,action:#selector(handleLikes(sender :)),for:.touchUpInside) –

+0

@saroshmirza沒有工作以及 –

回答

-1

我不知道爲什麼它不工作,但親自當我需要把一個按鈕在電池我用它引發的@IBAction委託,如:

import UIKit 
protocol MySuperTableViewCellDelegate: class { 
    func didPressCheckBoxButton(cell: MySuperTableViewCell) 
} 

class MySuperTableViewCell: UITableViewCell { 
    weak var delegate: MySuperTableViewCellDelegate? 

    @IBOutlet weak var checkBoxButton: UIButton! 

    @IBAction func checkBoxButtonPressed(_ sender: Any) { 
     if let d = delegate { 
      d.didPressCheckBoxButton(cell: self) 
     } 
    } 

    //Here is the default code like "awakeFromNib" etc 
} 

而且在你展示的tableview的視圖 - 控制當您創建「CellForRowA細胞牛逼Indexpath」我需要把 ‘cell.delegate =自我’ 所以,你的控制器是每個單元

的代表,並在你的ViewController的末尾:

extension MySuperViewController : MySuperTableViewCellDelegate { 
    didPressCheckBoxButton(cell: MySuperTableViewCell) { 
     print("checkbox button has been pressed") 
    } 
} 
+0

我真的很感激你的方式,但我的建議是使用塊更好,然後協議 –

+0

你能告訴我爲什麼嗎?這是一個更好的方式來做應用程序效率或類似的東西嗎?如果協議不好,我會開始使用其他方式,但我學會了這樣做 – Leo

+1

不是這樣的 但是,如果某件事情需要一個函數,因爲它的接口,回調(Clouser)通常是一個很好的解決方案。如果需要多個函數,特別是當它們需要對象的基本功能時,Delegate可能是更好的解決方案。 –