如何更改一个表格中按钮的颜色VIewCell [重复]
Posted
技术标签:
【中文标题】如何更改一个表格中按钮的颜色VIewCell [重复]【英文标题】:How to change the color of Button in one tableVIewCell [duplicate] 【发布时间】:2017-07-17 11:37:14 【问题描述】:我有多个 tableViewCells,每个 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)
但它会改变所有其他单元格中按钮的颜色。但我只需要在我按下的那个单元格中更改按钮颜色。 (我在 1 个单元格中有 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
有按钮的目标功能(我已经确定了按钮按下时的索引路径):
func variant1ButtonPressed(_ sender:AnyObject)
let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tableView)
let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
let intIndexpath = Int((indexPath?.row)!)
func variant2ButtonPressed(_ sender:AnyObject)
let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tableView)
let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
let intIndexpath = Int((indexPath?.row)!)
func variant3ButtonPressed(_ sender:AnyObject)
let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tableView)
let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
let intIndexpath = Int((indexPath?.row)!)
func variant4ButtonPressed(_ sender:AnyObject)
let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tableView)
let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
let intIndexpath = Int((indexPath?.row)!)
请帮忙!!!
【问题讨论】:
这和***.com/questions/45135516/…中的代码一样吗 【参考方案1】:首先你没有使用委托。
其次,您已经定义了您的类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()
或者简单地覆盖视图控制器中的函数并将按钮委托给自己。
【讨论】:
所以它不起作用。我建议你遵循两个答案。两者都说同样的话【参考方案2】:尝试更改代码中的以下更改
1.
var variant1 : CustomButton!
var variant2 : CustomButton!
var variant3 : CustomButton!
var variant4 : CustomButton!
2 将自定义类分配给单元格中的按钮
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
【讨论】:
【参考方案3】:您只需为所有按钮设置 4 个标签。标签的数量需要等于您拥有的按钮总数。因此,如果您有两个单元格和 8 个按钮,则标签应为 1...8。有很多数字,因此您需要在循环中创建它们,并且还需要在循环中查找特定的单击按钮。我还建议您在自定义单元类中执行此操作。
【讨论】:
以上是关于如何更改一个表格中按钮的颜色VIewCell [重复]的主要内容,如果未能解决你的问题,请参考以下文章