Ios 如何更改一个tableVIewCell中按钮的颜色

Ios 如何更改一个tableVIewCell中按钮的颜色,ios,swift,uitableview,uibutton,Ios,Swift,Uitableview,Uibutton,我有多个tableviewCell,每个tableviewCell包含4个按钮。按下按钮时,我需要更改按钮的颜色。我尝试对UIButton使用委托方法。像这样: import UIKit class CustomButton: UIButton { override var isHighlighted: Bool { didSet { if isSelected { backgroundColor = UIColor.lightGray

我有多个tableviewCell,每个tableviewCell包含4个按钮。按下按钮时,我需要更改按钮的颜色。我尝试对UIButton使用委托方法。像这样:

import UIKit

class CustomButton: UIButton {

override var isHighlighted: Bool {
    didSet {
        if isSelected {
            backgroundColor = UIColor.lightGray
        } else {
            backgroundColor = UIColor.init(red: 34/255, green: 89/255, blue: 128/255, alpha: 1.0)
        }
    }
}
}

但它是改变所有其他单元格中按钮的颜色。但是我只需要在我按下的单元格中更改按钮的颜色。(我在一个单元格中有4个按钮,我想更改我按下的按钮的颜色,并且只更改该单元格中的按钮) 我是如何实现我的按钮并向每个按钮添加目标功能的:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return Questions.count
}
var variant1 = UIButton()
var variant2 = UIButton()
var variant3 = UIButton()
var variant4 = UIButton()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "finalCell")!
    variant1 = cell.contentView.viewWithTag(1) as! UIButton
    variant2 = cell.contentView.viewWithTag(2) as! UIButton
    variant3 = cell.contentView.viewWithTag(3) as! UIButton
    variant4 = cell.contentView.viewWithTag(4) as! UIButton
        let questionTextView = cell.contentView.viewWithTag(5) as! UITextView
        questionTextView.text = "\(Questions[indexPath.row].content!)"
        variant1.addTarget(self, action: #selector(self.variant1ButtonPressed), for: .touchUpInside)
        variant2.addTarget(self, action: #selector(self.variant2ButtonPressed), for: .touchUpInside)
        variant3.addTarget(self, action: #selector(self.variant3ButtonPressed), for: .touchUpInside)
        variant4.addTarget(self, action: #selector(self.variant4ButtonPressed), for: .touchUpInside)
        return cell

}
按钮的目标函数(我已经确定了按钮按下时的indexpath):


请帮忙

首先,您没有使用委托

其次,您已经定义了类
CustomButton
,并将按钮设置为
UIButton

替换此项:

var variant1 = UIButton()
var variant2 = UIButton()
var variant3 = UIButton()
var variant4 = UIButton()
为此:

var variant1 = CustomButton()
var variant2 = CustomButton()
var variant3 = CustomButton()
var variant4 = CustomButton()

或者简单地覆盖视图控制器中的功能,并将按钮委托给self。

尝试更改代码中的以下更改

一,

2将customclass分配给单元格中的按钮

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "finalCell")!
    variant1 = cell.contentView.viewWithTag(1) as! CustomButton
    variant2 = cell.contentView.viewWithTag(2) as! CustomButton
    variant3 = cell.contentView.viewWithTag(3) as! CustomButton
    variant4 = cell.contentView.viewWithTag(4) as! CustomButton
        let questionTextView = cell.contentView.viewWithTag(5) as! UITextView
        questionTextView.text = "\(Questions[indexPath.row].content!)"
    variant1.addTarget(self, action: #selector(self.variant1ButtonPressed(_:)), for: .touchUpInside)
    variant2.addTarget(self, action: #selector(self.variant2ButtonPressed(_:)), for: .touchUpInside)
    variant3.addTarget(self, action: #selector(self.variant3ButtonPressed(_:)), for: .touchUpInside)
    variant4.addTarget(self, action: #selector(self.variant4ButtonPressed(_:)), for: .touchUpInside)
        return cell

}

您仅为所有按钮设置了4个标记。标签的数量必须等于按钮的总数。所以如果你有两个单元格和8个按钮,标签应该是1…8。有很多数字,因此您需要在循环中创建它们,并在循环中查找特定的单击按钮。我还建议您在自定义单元格类中这样做

这与中的代码相同-因此它不起作用。我建议你遵循这两个答案。两人说的都一样
var variant1 : CustomButton!
var variant2 : CustomButton!
var variant3 : CustomButton!
var variant4 : CustomButton!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "finalCell")!
    variant1 = cell.contentView.viewWithTag(1) as! CustomButton
    variant2 = cell.contentView.viewWithTag(2) as! CustomButton
    variant3 = cell.contentView.viewWithTag(3) as! CustomButton
    variant4 = cell.contentView.viewWithTag(4) as! CustomButton
        let questionTextView = cell.contentView.viewWithTag(5) as! UITextView
        questionTextView.text = "\(Questions[indexPath.row].content!)"
    variant1.addTarget(self, action: #selector(self.variant1ButtonPressed(_:)), for: .touchUpInside)
    variant2.addTarget(self, action: #selector(self.variant2ButtonPressed(_:)), for: .touchUpInside)
    variant3.addTarget(self, action: #selector(self.variant3ButtonPressed(_:)), for: .touchUpInside)
    variant4.addTarget(self, action: #selector(self.variant4ButtonPressed(_:)), for: .touchUpInside)
        return cell

}