如何更改tableview单元格swift3中的按钮背景颜色

时间:2017-06-01 12:22:13

标签: ios uitableview swift3 tableviewcell

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

enter image description here

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()
 }

4 个答案:

答案 0 :(得分:2)

@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(发件人: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里面收集视图

答案 1 :(得分:0)

好吧,我建议你从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)
}

答案 2 :(得分:0)

我就这样做了:

首先是功能:

router.get('/',function(req,res,next){  
    AgendaCompromisso.aggregate([
    {
        $lookup:
        {
            from: "profissionais", //use the name of database collection not mongoose model
            localField: "profissional",
            foreignField: "_id",
            as: "profissional_doc"
        }
    },

    {
        $unwind: "$profissional_doc"  //remove array
    },

    {
        $sort: {"profissional_doc": -1}  // or {"profissional_doc": 1} for ascending
    }
    ])
    .exec( (err,data) => {
        callback(err,data,res)
    })
});

然后我称之为:

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)
        }

希望这有帮助!

答案 3 :(得分:0)

您希望将按钮子类化为CustomButton或您想要调用它的任何内容。而且你需要创建一个委托来让你的viewcontroller了解最新情况。

创建一个接受整数的func。这将是您的按钮标记中的代码,并预先形成一些代码。 @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。请务必遵守您的viewcontroller,以便委托工作。