I have a custom UITableViewCell subclass. I have set the contentView of my cell subclass to
a custom UIView class in which i am overriding -drawRect: and doing all of the drawing there.
Also, I am setting cell.contentView.opaque = NO in order to achieve transparency in certain areas of the cell (unfortunately, a backgroud image behind the table must show thru each cell in certain parts to achieve a stylistic effect. i know this is a performance hit. it must be so).
Problem: I still see the default pretty blue gradient background being drawn behind my cell (in the transparent areas) when it is selected or highlighted (being pressed). This is obscuring the image behind the table, which is bad.
Goal: To prevent the blue gradient background from appearing, but still be able to inspect the cell.isSelected and cell.isHighlighted properties from within -[MyContentView drawRect:] to determine how to draw my own custom selection/highlighting.
What I’ve tried:
-
setting
cell.selectionStyle = UITableViewCellSelectionStyleNonehas the desired effect of preventing the pretty blue gradient selection background, but also prevents thecell.isSelectedandcell.isHighlightedproperties from being properly set, which means i cannot do my own custom selection/highlight drawing -
setting
cell.selectionBackgroundView = nilandcell.backgroundView = nilin the cell’s-initor-prepareForReusemethod does not prevent the blue gradient selection background -
setting
cell.selectionBackgroundView = nilin the-[MyContentView -drawRect:]method does have the desired effect of preventing the blue gradient selection background, but that seems very janky -
overriding -[UITableViewCell setSelected:animated:] to be a no-op. this does not have the desired effect of preventing the blue gradient selection background
An excellent resource on customizing UITableViews has been this post by Matt Gallagher. What you’ll want to do is set the selectedBackgroundView to a new view (instead of nil) that is either transparent or a UIImageView.