I'm trying to save button image inside button view. Here is briefly what my code looks like:

I have a UITableView with button in it. Whenever I press the button the image changes. I change the image using this code:

First I use:

cell.checkmarkButton.addTarget(self, action:
        #selector(subscribeTapped(_:)), for: .touchUpInside)

to recognize when the image is tapped. Then I use:

@objc func subscribeTapped(_ sender: UIButton) {
    selectedButton = String(sender.tag)

    if let ButtonImage = sender.image(for: .normal),
        let Image = UIImage(named: "WhiteCheckMarkButton"),
        ButtonImage.pngData() == Image.pngData()
    {
        sender.setImage( UIImage.init(named: "GreenCheckMarkButton"), for: .normal)
    } else {
        sender.setImage( UIImage.init(named: "WhiteCheckMarkButton"), for: .normal)
    }

Inside my subscribeTapped function to change the image. All good it changes the image but I can't seem to find out how to save once the image has changed. It seems really confusing to me. I can definitely do it if the image is not in a tableView using UserDefaults. But inside a tableView I have no idea what should I do.

0 Answers