我有一个类似测验应用的应用。在tableViewCell中,有4个按钮用于任务的变体。当用户按下按钮时我需要改变颜色,当我尝试在按钮的动作中改变颜色时,按钮的颜色在所有表视图单元格中都会改变。但是我需要改变,只有在我按下的地方。我已经尝试过编写委托了,但它给出了相同的结果,这就是我编写代理的代码:
class CustomButton: UIButton {
override var isHighlighted: Bool {
didSet {
if isHighlighted {
backgroundColor = UIColor.lightGray
} else {
backgroundColor = UIColor.init(red: 34/255, green: 89/255, blue: 128/255, alpha: 1.0)
}
}
}
}
我已经为按钮添加了目标操作。
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
if (numberOfVariants.count != 0) {
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(如果它有用):
func variant1ButtonPressed(_ sender:AnyObject) {
print("Variant1")
let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tableView)
let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
let intIndexpath = Int((indexPath?.row)!)
globalIndexPath = intIndexpath
}
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)!)
globalIndexPath = intIndexpath
}
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)!)
globalIndexPath = intIndexpath
}
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)!)
globalIndexPath = intIndexpath
}
答案 0 :(得分:1)
我认为你使用的代码比实际需要的代码多得多,所以这是使用closures
的另一种方法,我的故事板中只有一个viewController,内部添加了一个UITableView,一个名为{的customCell {1}}然后我将一个普通的UIButton链接到那个单元格作为@IBOutlet,就是这样,完整的实现就在这里
https://github.com/rmelian2014/SOButtonsTableViewCellQuestion
ButtonTableViewCell
import UIKit
@IBDesignable
class ButtonTableViewCell: UITableViewCell {
@IBInspectable var selectedColor : UIColor = UIColor.red
@IBInspectable var normalColor : UIColor = UIColor.blue
static let cellHeigth : CGFloat = 360
@IBOutlet var buttonsArray: [UIButton]!
var selectedText : String?{
willSet{
guard newValue != nil else{
return
}
var foundedIndex = -1
for (index,text) in self.texts.enumerated() {
if(text == newValue!){
foundedIndex = index
break
}
}
guard foundedIndex != -1 else
{
return
}
self.buttonsArray[foundedIndex].backgroundColor = selectedColor
}
}
var texts : [String] = []
var buttonActionClosure : ((_ selectedText:String?)->Void)?
func setupWithClosure(texts:[String],textSelected:String?,closure:((_ selectedText:String?)->Void)?)
{
self.texts = texts
for (index,button) in self.buttonsArray.enumerated(){
if(self.texts.count > index)
{
button.setTitle(self.texts[index], for: .normal)
}
button.tag = index
button.backgroundColor = self.normalColor
}
self.selectedText = textSelected
if(closure != nil)
{
self.buttonActionClosure = closure
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func buttonAction(sender:UIButton)
{
if(self.texts.count > sender.tag){
let newSelectedText = self.texts[sender.tag]
if(newSelectedText != self.selectedText){
self.selectedText = newSelectedText
}else
{
self.selectedText = nil
}
}
if(self.buttonActionClosure != nil)
{
self.buttonActionClosure!(self.selectedText)
}
}
override func prepareForReuse() {
super.prepareForReuse()
self.buttonActionClosure = nil
}
}
希望这有助于你
答案 1 :(得分:0)
在 cellForRowAt indexPath:方法中,您需要检查行的索引路径是否与所选按钮的索引路径相同。如果相同则需要显示突出显示的颜色,否则显示正常颜色。
所以保存buttonpressedmethod中全局变量中按下的按钮的索引路径。让valiable成为selectButtonIndexPath。
在 cellForRowAt indexPath:方法
中添加以下代码 if indexPath.row==selectedButtonIndexPath.row {
backgroundColor = UIColor.lightGray
} else {
backgroundColor = UIColor.init(red: 34/255, green: 89/255, blue: 128/255, alpha: 1.0)
}
当单击按钮以反映颜色的变化时,当然会重新加载您的tableview。
更新1:
您是否可以通过获取sender.tag然后更改if条件来尝试保存按钮按钮的标签
if indexPath.row==selectedButtonIndexPath.row {
if buttonTag == 1{
variant1.backgroundcolor = UIColor.lightGray
variant2.backgroundColor = UIColor.init(red: 34/255, green: 89/255, blue: 128/255, alpha: 1.0)
// and so on for all buttons.
}else if buttonTag == 2{
// same as above but for variant2
}
} else {
}