3

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 button from multiple cell is having the same effect . Please do help me !

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! subTableViewCell

    cell.catNameLabel.text = categoryNameArray[indexPath.row]

    cell.descLabel.text = descriptionArray[indexPath.row]

    cell.freqLabel.text = frequency[indexPath.row] + " at " + frequencyTime[indexPath.row]

    let url = "http://wiinnova.com/tenly/image/category_image/\(categoryImageArray[indexPath.row])"

    cell.img.imageFromServerURL(urlString: url)
    cell.button.tag = indexPath.row
    return cell 
}
Gaurav Saini
  • 177
  • 1
  • 1
  • 8

3 Answers3

3

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.

Anurag Sharma
  • 4,276
  • 2
  • 28
  • 44
  • I have tried this but at the same time a button in someother row is having the same affect – Shafeer Puthalan Aug 30 '17 at 07:53
  • Can you please update your question with that code you have tried? – Anurag Sharma Aug 30 '17 at 07:59
  • It's will not work when tableView Scroll Up-Down because cell will be reusable. – Govaadiyo Aug 30 '17 at 08:01
  • Yes, Sorry for that i need to get the cell here using `converPoint` function. See the updated answer – Anurag Sharma Aug 30 '17 at 08:03
  • yes when i Scroll Up-Down the cell is reusing ..so what i can to do to avoid it .. can i use tableview.reload() and where i can use it – Shafeer Puthalan Aug 30 '17 at 08:10
  • @TofaaniKaanudo you have a nice way, But we can even avoid using `button.tag` if you want. Check my updated answer – Anurag Sharma Aug 30 '17 at 09:18
  • @AnuragSharma - Yes but what will be happened when we scroll tableView and will be called **cellForRowAtIndexPath** method ? Button title and backedGround color will be reset again as normal so your updated effect will be gone. That's why we must need to use Array for handle updated effect. – Govaadiyo Aug 30 '17 at 09:22
  • @TofaaniKaanudo Yes, you are right for that we need an array to keep the track of selected/Unselected buttons :) – Anurag Sharma Aug 30 '17 at 09:27
3

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.

Govaadiyo
  • 5,644
  • 9
  • 43
  • 72
0

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
Willjay
  • 6,381
  • 4
  • 33
  • 58