mginn
mginn

Reputation: 16124

Proper way to highlight uicollectionviewcell

I have a uicollectionview, with subclassed uicollectionviewcells. I want to change the color of the cell when touched, then return to the default when the touch is released. Also, if I could animate this color change it would be optimal. I have tried implementing the didHighlightItemAtIndexPath method and changing the dequeued cell's backgroundColor property, but nothing appears to change. The only way I can accomplish this is to override the cell's touchesBegan method, but then didSelectItemAtIndexPath isn't called.

If I use didHighlightItemAtIndexPath, the method is called, but for some reason the background color is not changed.

I don't know what UICollectionViewCell's standard method is when touched, but is there some way I could run it?

Upvotes: 2

Views: 266

Answers (1)

gutenmorgenuhu
gutenmorgenuhu

Reputation: 2362

You have to configure the backgroundView of your dequeued cell in cellForItemAtIndexPath:

    let backgroundView = UIView(frame: CGRectMake(0, 0, cell.contentView.frame.width, cell.contentView.frame.height))
    backgroundView.backgroundColor = .blueColor()
    cell.selectedBackgroundView = backgroundView

Upvotes: 1

Related Questions