how to change the button title on click inside a tableview cell in swift 3

前端 未结 3 590
野趣味
野趣味 2021-01-01 06:28

I got a tableview cell inside that i have image view , label and a ui button , i need to change the button title text and background colour on click . But when i try to do

相关标签:
3条回答
  • 2021-01-01 06:46

    Set the title and the background color properties of the button in tableview's didSelectRowAtIndexPath delegate

    cell.button.setTitle("title", for: .normal)
    cell.button.backgroundColor = .darkGray
    
    0 讨论(0)
  • 2021-01-01 06:55

    You don't need to assign button.tag at all, Here is the way:

    First of all add target to your button. Like:

    cell.button.addTarget(self, action:#selector(handleRegister(_:)), for: .touchUpInside). 
    

    Then:

    //This function will be called for each button click. 
    
    func handleRegister(sender: UIButton) {
    
    let buttonPosition = sender.convert(CGPoint.zero, to: YOUR_TABLE_VIEW)
    let indexPath: IndexPath? = YOUR_TABLE_VIEW.indexPathForRow(at: buttonPosition)
    
    let cell = YOUR_TABLE_VIEW.cellForRow(at: indexPath as IndexPath)
    
    //Now change the text and background colour
    cell.button.setTitle("Button Title",for: .normal)
    cell.button.backgroundColor = UIColor.blueColor()
    
       //...
    }
    

    To keep the track of your changed button background and title you will need an array as @Tofaani Kaanudo's answer has suggested.

    0 讨论(0)
  • 2021-01-01 06:59

    First of all you should take one Array for manage clicked/changed UIButton for reusableCell otherwise if you scroll your tableView Up-Down then It will be remove effect

    I'm giving my logic.

    1) Take one mutable Array name is temArray (Size equal to your tableView's Row) and it has 0 value for each index. you can do it by easy repeat value 0.

    2) in you cellForRowAtIndexPath datasource method check

    if temArray[indexPath.row] == 0 {
       /// Write code for default UIButton - That has normal behavior means not changed title and no set background color
    }
    else {
      /// Write code for What you want to keep UIButton title and background color
    }
    
    cell.yourButtonObject.tag = indexPath.row
    cell.yourButtonObject.addTarget(self, action:#selector(handleButtonClicked(_:)), for: .touchUpInside). 
    

    3) And add handleButtonClicked method and change temArray value

    func handleButtonClicked(_ sender: UIButton) {
            let myIndexPath = NSIndexPath(row: sender.tag, section: 0)
            let cell = tblViewPref.cellForRow(at: myIndexPath as IndexPath)
            if temArray[sender.tag] == 0 {
            /// You can access your button by
            // cell.myButton..... change here text and background color
            // And change "temArray"
    
            temArray[sender.tag] = 1
           }
           else {
    
               /// You can access your button by
              // cell.myButton..... change here text and background color
              // And change "temArray"
    
             // SET DEFULT BUTTON TITLE AND BACKGROUND COLOR
    
             temArray[sender.tag] = 0
           }
        }
    

    So when you scroll your table Up-down temArray will be managed by it's value at indexPath in cellForRowAtIndexPath Datasource method.

    0 讨论(0)
提交回复
热议问题