如何在一个表中更改Button的颜色VieCell

时间:2017-07-17 11:37:14

标签: ios swift uitableview uibutton

我有一个多个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)!)
}

请帮助!!!

3 个答案:

答案 0 :(得分:0)

所有你没有使用委托的人。

其次,您已定义了课程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()

或者只是覆盖View Controller中的功能并将按钮委派给自己。

答案 1 :(得分:0)

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

1

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

}

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

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/searchfield"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@drawable/custom_search_edit"
 android:hint="@string/search_hint"
 android:inputType="textFilter"
 android:textColor="@color/colorBlack"
 android:textColorHint="@color/colorAccent"
 android:textSize="15sp" />

答案 2 :(得分:0)

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