I have a table view with 2 (or more) sections. I have added an ImageView into it and need to change the image view according the values which are containing in an array at the beginning and when selecting/deselecting a cell. I created the view as follows,
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let viewHeader = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40))
buttonCheck = UIButton(type: .custom)
buttonCheck!.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
buttonCheck!.tag = section
buttonCheck!.addTarget(self, action: #selector(tapSection(sender:)), for: .touchUpInside)
viewHeader.addSubview(buttonCheck!)
}
This adds the ImageView fine and when I load the table data initially, I need to set the image view programatically. To change the image view I did,
if tableViewData.contains(where: self.tags.contains) {
buttonCheck!.setImage(UIImage(named: "CheckmarkCircle"), for: .normal)
} else {
buttonCheck!.setImage(UIImage(named: "DeselectedCheckmarkCircle"), for: .normal)
}
I called this inside didSelectRowAt
and didDeselectRowAt
method. The problem here is, When I select a cell from section one(section = 0), it affects to the second section(section = 1) header image view. In other work when I select a cell from section one the second section's header image is changing. How may I fix this?