Trying to override "selected" in UICollectionViewCell Swift for custom selection state Trying to override "selected" in UICollectionViewCell Swift for custom selection state swift swift

Trying to override "selected" in UICollectionViewCell Swift for custom selection state


And for Swift 3.0:

override var isSelected: Bool {    didSet {        alpha = isSelected ? 0.5 : 1.0    }}


Figured it out by stepping into code. The problem was that the super.selected wasn't being modified. So I changed the code to this:

override var selected: Bool {    get {        return super.selected    }    set {        if newValue {            super.selected = true            self.imageView.alpha = 0.5            println("selected")        } else if newValue == false {            super.selected = false            self.imageView.alpha = 1.0            println("deselected")        }    }}

Now it's working.


Try this one.

override var selected: Bool {    didSet {        self.alpha = self.selected ? 0.5 : 1.0    }}