I have a tableview with multiple sections each section is having 3 cells , each cell contains the custom check mark button. Where user can change check and uncheck images of check button on click. The problem is, i am changing the cell button image from uncheck to check when user click on button which is working fine. If i scroll the tableview that check mark image is adding to wrong cell.
I googled it and found the solution like saving clicked cell indexPath.row in array and removing from array if user click again on same cell. It is not working as i have multiple sections.
Please provide me any suggestion to find out the solution for my problem.
// Button action.
func buttonTappedOnCell(cell: customeCell) {
let indexPath : IndexPath = self.tableView.indexPath(for: cell)!
if self.selectedIndexPath.count > 0 {
for selectedIndex in self.selectedIndexPath {
if selectedIndex as! IndexPath == indexPath {
self.selectedIndexPath.remove(indexPath)
} else {
self.selectedIndexPath.add(indexPath)
}
}
} else {
self.selectedIndexPath.add(indexPath)
}
Code in cellForRowAtIndexPath
for anIndex in self.selectedIndexPath {
if anIndex as! IndexPath == indexPath {
cell.checkMarkButton.setBackgroundImage(UIImage(named: "checkMark"), for: .normal)
} else {
cell.checkMarkButton.setBackgroundImage(UIImage(named: "UnCheckMark"), for: .normal)
}
}