繁体   English   中英

如何在TableView单元格swift3中更改按钮背景颜色

[英]how to change button background colour in tableview cell swift3

我有一个UITableViewCell 在TableViewCell中,我在单行中有7个按钮。 我想播放音乐。 当我按下button1时,其背景色应更改。 如果按了button2,则应更改其背景色,然后取消选择button1。 我也为行中的每个按钮设置标签,但我不知道该怎么办? 声音播放正常,但我无法选择和取消选择按钮。 我正在使用swift3 提前致谢。

在此处输入图片说明

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> tableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "PlayerCell") as! tableViewCell

     cell.Btn_one.addTarget(self, action: #selector(TableViewController.BtnAction_One), for: .touchUpInside)

     //For example
       if (cell.Btn_one.tag == 1){

        cell.Btn_one.backgroundColor = .white
    }else if (cell.Btn_two.tag == 2){
               cell.Btn_two.backgroundColor = .red
    }
 }


 @IBAction func BtnAction_One(_ sender: UIButton) {
    self.play()
 }

@Chetan解决方案:第一个

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellData", for: indexPath)

    let btn1 = cell.viewWithTag(1) as! UIButton
    let btn2 = cell.viewWithTag(2) as! UIButton
    let btn3 = cell.viewWithTag(3) as! UIButton
    let btn4 = cell.viewWithTag(4) as! UIButton
    let btn5 = cell.viewWithTag(5) as! UIButton
    let btn6 = cell.viewWithTag(6) as! UIButton

    btn1.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
    btn2.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
    btn3.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
    btn4.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
    btn5.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
    btn6.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)

    return cell
}

何时将btnClicked`func btnClickec(sender:UIButton){sender.isSelected = true

    let cell = sender.superview?.superview as! UITableViewCell

    let btn1 = cell.viewWithTag(1) as! UIButton
    let btn2 = cell.viewWithTag(2) as! UIButton
    let btn3 = cell.viewWithTag(3) as! UIButton
    let btn4 = cell.viewWithTag(4) as! UIButton
    let btn5 = cell.viewWithTag(5) as! UIButton
    let btn6 = cell.viewWithTag(6) as! UIButton

    let arr = [btn1,btn2,btn3,btn4,btn5,btn6];

    for index in 0...5{
        let btn = arr[index]
        if index+1 == sender.tag{
            btn.isSelected = true
        }else{
            btn.isSelected = false
        }
    }
}

`

第二个:或者您也可以在tableviewcell内收集视图

好吧,我建议您从UIButton继承类,如下所示:

class CustomButton: UIButton {

var isPressed = false

override init(frame: CGRect) {
    super.init(frame: frame)
}

convenience init(frame: CGRect, title: String, tag: Int) {
    self.init(frame: frame)

    self.frame = frame
    setTitle(title, for: .normal)
    setTitleColor(.white, for: .normal)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    switch tag {
        case 0: //Play a music
            if isPressed{
                backgroundColor = .black
            } else {
                backgroundColor = .clear
            }
        case 1: //Play a video
            if isPressed{
                backgroundColor = .black
            } else {
                backgroundColor = .clear
            }
    default: //Other
        if isPressed{
            backgroundColor = .black
        } else {
            backgroundColor = .clear
        }
    }

    if isPressed { isPressed = false } else { isPressed = true }
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

之后,在您的单元格中,我会喜欢...

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: .default, reuseIdentifier: nil)

    let musicB = CustomButton(frame: CGRect(x: 0, y: 0, width: 0, height: 0), title: "1", tag: 1)
    addSubview(musicB)
}

这是我的方法:

首先功能:

func switchColor(isWhite: Bool, sender: UIButton) {
        if isWhite == true {
            sender.backgroundColor = UIColor.clear
            sender.setTitleColor(UIColor.white, for: UIControlState.normal)
        }else if isWhite == false {
            sender.backgroundColor = UIColor.white
            sender.setTitleColor(UIColor.clear, for: UIControlState.normal)
        }

然后我称之为:

@IBAction func RepeatWeeklyPressed(_ sender: UIButton) {
        if sender.backgroundColor == UIColor.white {
            switchColor(isWhite: true, sender: sender)
        }else {
            switchColor(isWhite: false, sender: sender)
        }
    print("Pressed a repeat button")
    }

希望这可以帮助!

您需要将按钮归类为CustomButton或您想调用的任何子类。 而且,您需要创建一个委托以使您的视图控制器知道最新情况。

创建一个接受整数的函数。 这将是将包含在按钮标签中并执行一些代码的代码。 @Gabs在这里有正确的想法。

func select(index: Int) {
  switch sender.tag {
        case 0: //do something
        case 1: //do something
        }
}

创建一个IBAction,然后将所有按钮链接到它。 在内部,调用上面创建的方法

 @IBAction func buttonTapped(_ sender: Any) {
  select(index: sender.tag)
}

所缺少的只是viewController被告知点击了哪个按钮。 在CustomCell视图文件中为其设置一个委托,并使其在IBAction中捕获sender.tag。 确保也符合您的视图控制器的要求,以便进行委派。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM