English 中文(简体)
自定义UITableViewCell,带drawRect+普通蓝色选择
原标题:Custom UITableViewCell with drawRect + normal blue selection

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:

  • Blue gradient selection color
  • Inverted text color
  • Animated deselection

我没能做到这三点。

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.

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.

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作为子类。

This post has a nice description of how to do it:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

问题回答

我建议将第二次尝试与手势识别器结合使用。检查用户是否按下单元格,如果按下,请将该单元格中的文本颜色设置为黄色(或者蓝色的反转是什么?xD)。

示例代码:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view isKindOfClass:[UIButton class]]) {
        //do something with that view, for example: 
        //change the color of the text in that view, 
        //or invert it ;)
        return YES;
    }
    return NO;
}

或者,您可以将UIGestureRecognizer附加到该按钮。这样,就可以在选择器中插入调用的类。

按照你的要求去做可能会有更好的选择。我刚刚想到,如果你已经有2/3的分数在工作,这应该会帮助你获得3/3。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签