【问题标题】:Change Image Button in cell of table view更改表格视图单元格中的图像按钮
【发布时间】:2018-04-19 13:07:50
【问题描述】:

我有两个类TableViewControllerCustomCell 然后将IB 中的按钮(likeBtn) 连接到CustomCell

现在,我想更改此按钮的图像!

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"

        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomCell

        // Configure the cell...            
        cell.likeBtn.addTarget(self, action: #selector(TableViewController.liked), for: .touchUpInside)

        return cell
    }

@objc func liked(sender: UIButton) {

        cell.likeBtn.setImage(UIImage(named: "liked.png"), for: .normal)
        cell.likeBtn.isUserInteractionEnabled = false
    }

但是在liked() 中,我有关于cell.likeBtn 的错误。

我正在使用 swift 4

进行编程

【问题讨论】:

    标签: swift uitableview button cell


    【解决方案1】:

    正确的做法应该是:

    @objc func liked(sender: UIButton) {
        sender.setImage(UIImage(named: "liked.png"), for: .normal)
        sender.isUserInteractionEnabled = false
    }
    

    【讨论】:

    • 嗨,谢谢,我想到了sender.tag 或传递参数,哈哈。现在我遇到了一个 BUG,即单击第一个单元格时,在表格中单击了一些单元格!!
    • 如果您只想点击某个单元格按钮,只需在 cellForRowAt 中执行此操作: if indexPath.row == (which row) {cell.likeBtn.addTarget(self, action: #selector(TableViewController .liked),用于:.touchUpInside)}
    • 谢谢,但我想点击表格中的任何单元格并将其保存为只有我点击了!
    • 你可以随时添加标签,你的标签可以是indexPath.row,这样每个按钮都有一个唯一的标签
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多