views:

4687

answers:

5

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:

  1. setting cell.selectionStyle = UITableViewCellSelectionStyleNone has the desired effect of preventing the pretty blue gradient selection background, but also prevents the cell.isSelected and cell.isHighlighted properties from being properly set, which means i cannot do my own custom selection/highlight drawing

  2. setting cell.selectionBackgroundView = nil and cell.backgroundView = nil in the cell's -init or -prepareForReuse method does not prevent the blue gradient selection background

  3. setting cell.selectionBackgroundView = nil in the -[MyContentView -drawRect:] method does have the desired effect of preventing the blue gradient selection background, but that seems very janky

  4. overriding -[UITableViewCell setSelected:animated:] to be a no-op. this does not have the desired effect of preventing the blue gradient selection background

+10  A: 

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.

Jason
Jason, thank you! your suggestion has done the trick. a couple of notes:- I'm not sure how you gleaned that info from the article as it was not one of the significant points made. but good job :)- that article is def very informative. unfortunately, it also seems a litle misleading in that it ignores the common case where u need more cell customization in which case it is better to subclass and override the contentView's -drawRect: to achieve fast scrolling as explained here:http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/
Todd Ditchendorf
Just to further clarify this for future readers, instead of setting any cell.selectionStyle, set cell.selectedBackgroundView to a UIView - I set it to a UIView with the same bounds as my cell, and then set alpha to 0.5 to highlight the cell but still leave everything visible. Don't forget to call [self setNeedsDisplay] in your setSelected method!
Kendall Helmstetter Gelner
+1  A: 

What has worked for me in the past is just putting:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { }

In my UITableViewCell subclasses (because it won't call super and make itself highlighted). Hope this is what you were looking for.

thx joe, but i've tried that and it does not prevent the blue gradient seletion background
Todd Ditchendorf
i edited my Q above to include that i tried this and it did not have the desired effect
Todd Ditchendorf
+2  A: 

You must also override setHighlighted: to prevent the blue gradient from ever showing. If you just override setHighlighted: then you end up with a momentary selection effect.

so you'll have these two methods:

- (void)setHighlighted: (BOOL)highlighted animated: (BOOL)animated
{
    // don't highlight
}

- (void)setSelected: (BOOL)selected animated: (BOOL)animated 
{
    // don't select
    //[super setSelected:selected animated:animated];
}
jessecurry
This is by far the best answer. More people need to upvote it.
Sam V
+1  A: 
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Mugunth Kumar
Doesn't work as noted in the question.
Sam Soffes
A: 

How about this?

// disable user interaction
cell.userInteractionEnabled = FALSE;

-- Liam

Liam Goodacre