使用drawRect +普通蓝色选择的自定义UITableViewCell [英] Custom UITableViewCell with drawRect + normal blue selection
问题描述
我有一个自定义的UITableViewCell.所有绘图均在drawRect:方法中完成,并且未添加任何子视图.绘图效果很好,滚动速度也不错.但是问题出在所选单元格上.我希望选择的行为像往常一样:
I have a custom UITableViewCell. All the drawing is done in the drawRect: method and no subviews are added. The drawing works fine and the scroll speed is good. But the problem is with the selected cells. I want the selection to behave like it would normally:
- 蓝色渐变选择颜色
- 反转的文本颜色
- 动画取消选择
我还没有实现全部三个.
I have not been able to achieve all three.
首次尝试:
设置selectionStyle = UITableViewCellSelectionStyleNone并在drawRect方法中进行选择颜色.使用这种方法,我可以实现前两件事,但是取消选择是即时的.因此,一秒将其选择为蓝色,下一秒将其取消选择.我没有得到很好的淡出效果.
First attempt:
Set selectionStyle = UITableViewCellSelectionStyleNone and do the selection color in the drawRect method.
With this method, I am able to achieve the first two things, but the deselection is instant. So one second it is selected blue, the next second it is deselected. I do not get the nice fade-out.
第二次尝试:
selectionStyle = UITableViewCellSelectionStyleBlue使用此方法时,选中时,我的单元格全部为渐变蓝色.我的文字不可见.不过,淡出效果很好.
Second attempt:
selectionStyle = UITableViewCellSelectionStyleBlue
With this method my cell is all gradient blue when selected. The text I have is not visible. The fade-out works nicely, though.
第三次尝试:
设置selectionStyle = UITableViewCellSelectionStyleBlue,还将selectedBackgroundView设置为UIView,其中将backgroundColor设置为透明的蓝色.这里的问题是在我的普通内容之上绘制了selectedBackgroundView(尽管有名称).因此,如果selectedBackgroundView不透明,则看不到该文本.如果它是透明的,我可以看到文本,但是选择颜色会褪色"并且看起来不正确.
Third attempt:
Set selectionStyle = UITableViewCellSelectionStyleBlue and also set selectedBackgroundView to a UIView where I set the backgroundColor to a transparent blue.
Here the problem is that the selectedBackgroundView (despite the name) is drawn on top of my normal content. So if the selectedBackgroundView is not transparent, I cannot see the text. And if it is transparent, I can see the text, but the selection color gets "faded" and does not look right.
实现所有这三个要点的正确方法是什么?
推荐答案
确定.因此,这是我自己的问题的答案...不要将UITableViewCell子类化为自定义绘图.而是将UIImageView或UIView子类化.
OK. So the answer to my own question... Don't subclass UITableViewCell for custom drawing. Instead subclass a UIImageView or a UIView.
这篇文章对如何做有很好的描述:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
This post has a nice description of how to do it:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
这篇关于使用drawRect +普通蓝色选择的自定义UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!