Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

iPhone: drawRect in UiTableViewCells

forum search link has vanished for me, soo..

I'm am trying to optimise drawing UITableViewCells, in which the sample in TableViewSuite says to do direct drawing, but no examples of how.

I've tried this in my custom UITableViewCell class, but nothing appears.

- (void)drawRect:(CGRect)rect {
[[UIColor blackColor] set];
[storyTitle drawInRect:CGRectMake(boundsX, CONTROL_TWEENING, titleWidth, STORY TITLEHEIGHT) withFont:[UIFont systemFontOfSize:18]];
}

(storyTitle being a NSString object)

What am I missing here? (the above drawInRect method worked in one of my other views)

Posted on May 20, 2008 9:06 PM

Reply
6 replies

May 21, 2008 1:50 PM in response to HeoU

actually, you should call [super drawRect:rect] in the beginning of your drawRect method.

This is what i did, and it does wonders:

- (void)drawRect:(CGRect)Rect {
CGContextRef ref = UIGraphicsGetCurrentContext();
[super drawRect:rect];
CGContextSetRGBFillColor(ref, 0.0f, 0.0f, 0.0f, 1.0f); // set color to black again after super.
CGRect writeInHere = CGRectMake(0,0,100,20);
[@"teststring" drawInRect:writeInHere withFont:[UIFont fontWithName:@"Helvetica"] lineBreakMode:UILineBreakModeTailTruncation];
}

you need to call super as the first thing, esp if you want to support "selected" cells.
you will want to create the font instance outside the draw method for optimization.

- Jae

May 21, 2008 7:38 PM in response to damienl

hi all thanks for the help.

i've found the problem. I'm using the table style UITableViewStyleGrouped and my text is being drawn to the background view, and not in the contentView(?) (which I think is the white rounded rectangle on top) which is on top and hiding my text. So, should i add a custom subview to contentView (as suggested by the docs) and do the drawing in there?

PS: seems to not matter where I call the super function, BTW

May 22, 2008 12:07 AM in response to damienl

Yes, if you use UITableViewStyleGrouped, i should do


- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)identifier
{
if (self = [super initWithFrame:frame reuseIdentifier:identifier]) {
// Init your text here
}
return self;
}


and rewrite


- (void)layoutSubviews
{
[super layoutSubviews];
CGRect contentRect = [self.contentView bounds];

CGRect frame = CGRectMake(contentRect.origin.x, 6, 200, 80);
yourtext.frame = frame;
}


Good luck,

cheers,

HeoU

iPhone: drawRect in UiTableViewCells

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.