【问题标题】:how to change button background colour in tableview cell swift3如何在tableview单元格swift3中更改按钮背景颜色
【发布时间】:2017-11-02 14:18:51
【问题描述】:

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

【问题讨论】:

    标签: ios uitableview swift3 tableviewcell


    【解决方案1】:

    您需要将按钮子类化为 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。确保也符合您的视图控制器,以便委托工作。

    【讨论】:

      【解决方案2】:

      我就是这样做的:

      首先是函数:

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

      希望这会有所帮助!

      【讨论】:

        【解决方案3】:

        @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 中收集视图

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-26
            • 1970-01-01
            • 2018-09-30
            • 1970-01-01
            • 2015-06-04
            • 1970-01-01
            相关资源
            最近更新 更多