Problem drawing outside of drawRect: in a custom table cell
Problem drawing outside of drawRect: in a custom table cell
- Subject: Problem drawing outside of drawRect: in a custom table cell
- From: Graham Cox <email@hidden>
- Date: Wed, 10 Dec 2008 21:44:25 +1100
I need to draw outside of the usual drawRect: method (no really, I do!)
The reason is that I have a custom cell that is used in a table/
outline view that draws a colour swatch. When clicked it pops up a
menu of colours so the user can choose another colour. While tracking
the menu, the cell shows the colour in the menu that the mouse is
over. When the mouse is released, the cell sends the chosen colour to
the table's datasource as usual.
The drawing of the colour while tracking the menu needs to take place
outside the drawRect: mechanism. Why? Because the host view is a
table, and I need to avoid it reloading the cell from the datasource
while I'm tracking the menu. If I invalidate the table view, it will
redraw the entire row. That would be OK if I could rely on it to draw
only my row, but in some circumstances I have found that it doesn't,
but redraws the whole table (for example after editing a text cell,
for some reason any setNeedsDisplayInRect: on a small part of the
table causes the whole table to reload). That stuffs up the cell
because its colour value can get switched out (to nil or another
colour) while it should really reflect what is being tracked in the
menu or was just chosen from the menu. (I have tried a whole lot of
tricks to try to make it work that way, believe me, but it's so
convoluted and I have so little control over what is happening in the
tableview that I have given up and am trying to work it by short-
circuiting the cell drawing, but only while tracking).
So anyway, things are almost working fine - in fact they do work fine
at all times except the very first use of the cell. If the table is
used in any way prior to clicking the cell (selecting any row, for
example), it works fine. But if the first ever click on the table is
on my cell, I get no drawing - it's as if the cell is clipped out
(though forcing the clip to my cell's rect doesn't fix this so it's
not actually that). For my cell to work, the table has to have done
something other than draw its rows.
I draw my cell like this:
- (void) drawImmediately
{
[mControlView lockFocus];
[self drawInteriorWithFrame:mFrame inView:mControlView];
[mControlView unlockFocus];
[[mControlView window] flushWindow];
}
where <mControlView> and <mFrame> are ivars that are set on entry to
the mousetracking method to be the hosting control view and the cell's
frame respectively. I have verified that mControlView is indeed always
valid and the correct object at this point. The cell rect is also good.
Maybe it s a graphics context problem? I have tried always setting the
current context to the view's window but that stops it from ever
working (which may be a clue). Anyone spot anything I'm doing or not
doing here?
Also, if you feel tempted to advise me not to do it this way, believe
me, I have exhaustively investigated that route, which has proven even
more problematic, because there's insufficient fine control over what
the tableview itself does in its own drawRect: method. So far this
approach is the cleanest by far.
tia,
Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden