views:

508

answers:

5

I'm implementing a UIView's (UITableViewCell to be more exact) drawRect method.
My view has a transparent background, so when something is changed in the view, the older drawn version still remains there. So if on the first drawRect draw an "A", then a "B" on the same point, I get both of them drawn on top of each other.

How can I tell the framework to redraw the background?
(which I suppose it doesn't do because is not always needed, but in this case it badly is)

I guess what I need is the equivalent of win32's invalidateRect, however I went thru UIViews members and didn't find anything.

Btw, i've tried setNeedsDisplay, it didn't help.

+1  A: 

you should set clearsContextBeforeDrawing

CiNN
tried it, didn't work
Prody
A: 

I wonder if, since it is your own view that is retaining the drawing, whether you yourself should erase the rect being passed in?

What happens if your background is not transparent?

mahboudz
if my background is any opaque color, it gets drawn, A is erased and only B is shown.
Prody
A: 

I'm no longer reusing cells.

This is working for me because I always have < 20 cells in my table. I guess this is not a solution for everyone, but it's the way I'm planning to go.

Rather than have the framework create and reuse 10ish cells, I'm creating my 20 while fetching the data for them, and later at display time things go smoother because they don't need to be re-customized every time.

Prody
A: 

It's possible having come from the Windows world that you're thinking about the problem wrong. Having also come from the Windows world, I've done the same many times myself. Why do you need to override drawRect? Whenever you do that, you are responsible to take care of everything. Is it possible to do what you are wanting another way? What are you drawing in drawRect? Can you just add sub views or sub layers to your cell instead?

BTW, are you calling [super drawRect:rect] at the beginning of your drawRect override?

Matt Long
+3  A: 

I think I've used CGContextClearRect(CGContextRef context, CGRect rect) for this before.

Thomas Müller