swift - Tableview cell button selection error -


i have tableview contains button in left of cell. run works , when select button made change image background. problem when select buttonbuttonone selection , select other buttons when scroll tableview down, other buttons selected code way:

func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = self.tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! playercell     cell.btnadd.tag = indexpath.row     cell.btnadd.addtarget(self, action: #selector(createteamtwo.playeradded(_:)), forcontrolevents: .touchupinside)     return cell   }  func playeradded(sender: uibutton){     // let buttontag = sender.tag     sender.setimage(uiimage(named: "btn_buy_minus.png"), forstate: uicontrolstate.normal)         }  

can me please ?

take 1 array store selected indexpaths buttons tags. , in selector methods store sender tags , reload tableview. , in cellforrow method check if current indexpath contain in selected array show minus image otherwise show plus image.

var selected = [int]()   func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = self.tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! playercell     cell.btnadd.tag = indexpath.row     cell.btnadd.addtarget(self, action: #selector(createteamtwo.playeradded(_:)), forcontrolevents: .touchupinside)      if selected.contains(indexpath.row) {         cell.btn.setimage(uiimage(named: "btn_buy_minus.png"), forstate: .normal)     } else {         // here set plus image         cell.btn.setimage(uiimage(named: "btn_buy_plus.png"), forstate: .normal)     }     return cell }  func playeradded(sender: uibutton){     selected.append(sender.tag)     self.tableview.reloaddata() } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -