Reputation:
I setup a custom UITableViewCell and am subclassing it from UITableViewCell. I am using it in my table and everything works except when I select the cell, it highlights the default blue but the text is gone. Once I diselect the text is back. Does anyone know how to fix it?
This is the custom cell code I am using
#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
Upvotes: 1
Views: 1041
Reputation: 1306
Apple advises againts overriding UITableViewCell's -drawRect. If you really want to do custom drawing, do it in UIView subclass and then add the instance of that class as UITableViewCell's content view. Also Apple encourages using UIImageView as cell's background view instead of custom-drawn UIView subclasses (where possible, of course). That applies not only to cell's background view. As far as I see it, it's possible to use UIImageView in your case since drawing code in -drawRect is not parametrized in any way, that is, doesn't have external dependencies.
May be you should take a look at that: http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
But if you don't want to take another approach, there's a highlightedTextColor property of UILabel
Upvotes: 1
Reputation: 8520
See: UITableViewCell: How to prevent blue selection background w/o borking isSelected property?
Upvotes: 1
Reputation: 12106
Check your settings for UITableViewCellStyle, and UITableViewCellSelectionStyle. They're explained in the UITableViewCell Class Reference. The default selection style has a blue background which may be "masking" your text while in that state.
Upvotes: 0