Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

iphone - UITableViewCell: How to prevent blue selection background w/o borking isSelected property?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...