按钮目标不会执行TableViewCell中的函数

问题描述:

我试图按下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没有工作以及 –

我不知道为什么它不工作,但亲自当我需要把一个按钮在电池我用它引发的@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可能是更好的解决方案。 –