简体   繁体   中英

Custom button image in tableview cell isnt changing?

Ive got a custom tableview with a label and button. When the button is pressed on an individual cell, the image should change. But it doesnt?

so it logs "testing" when I click the individual custom tableview cell button, but the image doesnt change.

@IBAction func tickAction(sender: UIButton) {
    println("testing")

    if let image = UIImage(named:"Unchecked.png") {
        sender.setImage(UIImage(named:"Checked.png"), forState: .Normal)

    }
    if let image = UIImage(named:"Checked") {
        sender.setImage(UIImage(named:"Unchecked.png"), forState: .Normal)
    }
}

Your condition is wrong. Which image you are checking?

try to do it like:

@IBAction func tickAction(sender: UIButton) {
    println("testing")

    if (sender.selected) {
        sender.setImage(UIImage(named:"Unchecked.png"), forState: .Normal)
        sender.selected = false
    }
    else {
        sender.setImage(UIImage(named:"Checked.png"), forState: .Normal)
        sender.selected = true
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM