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.

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.

FWIW.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to