Iphone 自定义UITableViewCell未高亮显示文本

Iphone 自定义UITableViewCell未高亮显示文本,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我设置了一个自定义UITableViewCell,并从UITableViewCell中将其子类化。 我在我的表格中使用它,除了我选择单元格时,它会高亮显示默认的蓝色,但文本不见了。一旦我取消选择,文本就会返回。有人知道怎么修吗 这是我正在使用的自定义单元格代码 #import "TableViewCell.h" @implementation TableViewCell @synthesize text; - (id)initWithStyle:(UITableViewCellStyle)

我设置了一个自定义UITableViewCell,并从UITableViewCell中将其子类化。 我在我的表格中使用它,除了我选择单元格时,它会高亮显示默认的蓝色,但文本不见了。一旦我取消选择,文本就会返回。有人知道怎么修吗

这是我正在使用的自定义单元格代码

#import "TableViewCell.h"

@implementation TableViewCell

@synthesize text;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

    }
    return self;
}

- (void)setText:(NSString *)string {
    text = [string copy];
    [self setNeedsDisplay];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}


- (void)drawRect:(CGRect)rect {
    CGContextRef c = UIGraphicsGetCurrentContext();

    CGGradientRef myGradient;
    CGColorSpaceRef myColorspace;

    size_t num_locations = 2;
    CGFloat locations[2] = {0.0, 1.0};
    CGFloat components[8] = {0.8f, 0.8f, 0.8f, 1.0f, // Bottom Colour: Red, Green, Blue, Alpha.
        0.9f, 0.9f, 0.9f, 1.0}; // Top Colour: Red, Green, Blue, Alpha.

    myColorspace = CGColorSpaceCreateDeviceRGB();
    myGradient = CGGradientCreateWithColorComponents (myColorspace, components,
                                                      locations, num_locations);

    CGColorSpaceRelease(myColorspace);

    CGPoint startPoint, endPoint;
    startPoint.x = 0.0;
    startPoint.y = self.frame.size.height;
    endPoint.x = 0.0;
    endPoint.y = 0.0;
    CGContextDrawLinearGradient (c, myGradient, startPoint, endPoint, 0);

    const CGFloat topSepColor[] = { 0.8f, 0.8f, 0.8f, 1.0f }; // Cell Seperator Colour - Top

    CGGradientRelease(myGradient);

    CGContextSetStrokeColor(c, topSepColor);

    CGContextMoveToPoint(c, 0.0, 0.0);
    CGContextSetLineWidth(c, 1.0);
    CGContextSetLineCap(c, kCGLineCapRound);
    CGContextAddLineToPoint(c, self.frame.size.width, 0.0);
    CGContextStrokePath(c);

    const CGFloat bottomSepColor[] = { 0.5f, 0.5f, 0.5f, 1.0f }; // Cell Seperator Colour - Bottom
    CGContextSetStrokeColor(c, bottomSepColor);

    CGContextMoveToPoint(c, 0.0, self.frame.size.height);
    CGContextSetLineWidth(c, 1.0);
    CGContextSetLineCap(c, kCGLineCapRound);
    CGContextAddLineToPoint(c, self.frame.size.width, self.frame.size.height);
    CGContextStrokePath(c);

    [[UIColor blackColor] set];
    [text drawInRect:CGRectMake(10, self.frame.size.height / 3.5, self.frame.size.width - 20, self.frame.size.height - 10) withFont:[UIFont boldSystemFontOfSize:15] lineBreakMode:UILineBreakModeWordWrap];
}


- (void)dealloc {
    [super dealloc];
    [text release];
}


@end

检查UITableViewCellStyle和UITableViewCellSelectionStyle的设置。在UITableViewCell类参考中对其进行了解释。默认选择样式的背景为蓝色,在该状态下可能会“屏蔽”文本。

请参阅:

Apple建议再次覆盖UITableViewCell的-drawRect。如果确实要进行自定义绘图,请在UIView子类中进行,然后将该类的实例添加为UITableViewCell的内容视图。苹果还鼓励使用UIImageView作为单元格的背景视图,而不是自定义绘制的UIView子类(当然,在可能的情况下)。这不仅适用于单元格的背景视图。在我看来,在您的案例中使用UIImageView是可能的,因为-drawRect中的绘图代码没有任何参数化,也就是说,没有外部依赖关系

也许你应该看看:


但是如果你不想采取另一种方法,UILabel有一个highlightedTextColor属性

好的,我做了
cell.selectionStyle=UITableViewCellSelectionStyleNone
来消除高亮显示,但是我如何使它高亮显示并且文本可见?我认为@Russian找到了正确的方法。如果你提前知道你的单元格高度,你可以制作一个图形图像(在PhotoShot等中),将它放在UIImageView中,并将其设置为cell.backgroundView。然后,只需使用常规UITableViewCell并根据需要向其添加UILabels。如果你直到运行时才知道你的单元格高度,你必须在图像中绘制一个渐变,将该图像添加到imageView中,并将其设置为单元格的背景视图。哦,好的。我刚刚在博客上找到了这段代码,并实现了它。如果有更好的办法,我会去的。我只是在找一个简单的梯度,没什么疯狂的。