【问题标题】:Button Target doesn't execute function in TableViewCell按钮目标不执行 TableViewCell 中的功能
【发布时间】:2017-08-08 13:23:32
【问题描述】:

当按下 tableview 上的按钮时,我正在尝试执行某些操作。我将按钮添加到故事板中的单元格并像这样连接它:

@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}

【问题讨论】:

  • 请在 UITableViewCell 类上编写方法 handleLikes。
  • 尝试使用它,因为它属于同一类:cell.likeMessage.addTarget(self, action: #selector(handleLikes(sender:)), for:.touchUpInside)
  • @saroshmirza 效果不佳
  • @Fabioha 你能把整个代码sn-p分享到func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
  • @saroshmirza 我用整个代码 sn-p 更新了帖子

标签: ios swift uitableview uibutton


【解决方案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
}

在视图控制器中,当您在“CellForRowAt Indexpath”中创建单元格时,我需要在其中显示表格视图 “cell.delegate = self” 所以你的控制器是每个单元的代表

在你的视图控制器的末尾:

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

【讨论】:

  • 我真的很感谢你,但我的建议是使用块比协议更好
  • 你能告诉我为什么吗?这是提高应用程序效率的更好方法还是类似的方法?如果协议不好,我会开始使用其他方式,但我学会了这样做
  • 不是这样 但是,如果某些东西需要一个函数作为它的接口,回调(Clouser)通常是一个很好的解决方案。如果需要多个函数,尤其是对象的基本功能需要它们时,委托可能是更好的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 2013-11-02
  • 2013-05-01
  • 1970-01-01
相关资源
最近更新 更多