views:

491

answers:

3

Hi there,

I'm trying to draw a line on a custom UITableViewCell. When I draw inside the overriden drawRect and use the current graphics context I don't see anything.

I know I can't see anything because the draw rect is drawing to the UIView and not the contentView. So my question is, how do I draw to the content view using CG? Say, grabbing the graphics context to the contentView?

Thanks for the help.

+1  A: 

I have played with this in the past and what i have ended up doing is creating my own subclass of UIView and adding iot to a custom subclass of UITableViewCell and drawing to it that way. I like the control that this gives me.

Aran Mulholland
A: 

For some reason (I don't know why, but I did notice this) if you "custom-draw" (ie, redefine drawRect) on what's in the 'contentView' of your cell, it does not show up on the table cell. However, the view that you assign to cell.backgroundView and cell.selectedBackgroundView show up perfectly well...

You could put your custom view in cell.backgroundView, put nothing in cell.contentView and it will show up fine (except for when you select the row, because then your view disappears and is replaced by cell.selectedBackgroundView).

So, what you can do is:

  • Use cell.contentView to show a custom view without any background
  • Use cell.backgroundView and cell.selectedBackgroundView to show a fancy background for your cell...

Or:

  • Make your custom view flexible enough so that it can show both the selected and non-selected state, and use 2 instances of your custom view: one in cell.backgroundView and one in cell.selectedBackgroundView
Zoran Simic
A: 

I found my answer here. Basically the super class ABTabeViewCell sets up the context so you can easily draw in the drawContentView funtion.

Meroon