Re: Drawing the background of a single row in NSTableView
Re: Drawing the background of a single row in NSTableView
- Subject: Re: Drawing the background of a single row in NSTableView
- From: Peter Zegelin <email@hidden>
- Date: Thu, 2 Jul 2009 10:06:06 +1000
On 01/07/2009, at 6:12 PM, Quincey Morris wrote:
On Jul 1, 2009, at 00:26, Peter Zegelin wrote:
I need to draw the background of a single row in a tableview. This
row is not the selected row, and it changes often. I have more or
less got it working but it seems like a bit of a hack so I'm
wondering if there is a better way. I've also checked various
examples but most want to draw the selected rows differently, not
an arbitrary row.
At the moment I subclass the cell for each of the columns of the
view ( same subclass). In my delegates objectValueForTableColumn I
set a variable 'shouldDrawRow' to true if that is the 'special' row
I want to draw myself.
In the cell subclass I override drawInteriorWithFrame and check to
see if this value is set in my delegate and if so call a delegate
method to draw the background first.
This seems like a real hack as it assumes that
drawInteriorWithFrame will be called straight after
objectValueForTableColumn. This is sort of reasonable because the
cell is reused all the time so it 'works'. The other problem though
is I want to draw the ends of the rows differently to the middle
( thin rounded rectangle ) and drawInteriorWithFrame only gives me
the rectangle to draw in. I've got no way of finding out which
column it is. Again it will work because the columns won't change
so I can hard code some values.
However I'm thinking there must be a better way. What I really need
is some way of finding out the column and row of the cell about to
be drawn, but I can't see it. Am I missing something?
Such questions are hard to answer intelligently, because too much
depends on the details of your implementation, but that's never
stopped me from answering unintelligently before ...
I think it is a hack, and what you *really need* is to use your
actual data model.
That is, the objectValue for the columns in your special row need to
be recognizable as something different from the object values in the
non-special rows. Then your custom cell subclass, you test to see
whether its current objectValue is special, and draw it specially if
so. No fuss, no hacks, no hard-coded column assumptions.
OK, thanks for that. I hadn't thought of that way.
For example, if the regular object values were NSNumber objects, and
the special row values were column totals, I'd wrap the NSNumber
totals inside a custom object and put those in the data model
instead. If that would mess up your data model for other purposes,
I'd have objectValueForTableColumn detect the special row index and
return the wrapped NSNumber when the row index dictates.
Yes, it will mess up the data model so I'll have to do it in
objectValueForTableColumn. The data model is a simulator for AVR
microcontrollers, which I have written in C++. I am just trying to
draw a nice background for the current PC in the disassembled
instruction view, rather than drawing the usual pointer.
Thanks for your help. At least I know I am not missing something
obvious.
It does seem a bit of an oversight that there isn't something like
drawBackground: column: row.
cheers,
Peter
www.fracturedsoftware.com/rondo/ - MIDI for your mac
www.fracturedsoftware.com/macsimavr/
_______________________________________________
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