Problems subclassing NSTableView and NSOutlineView
Problems subclassing NSTableView and NSOutlineView
- Subject: Problems subclassing NSTableView and NSOutlineView
- From: Todd Blanchard <email@hidden>
- Date: Wed, 06 Jul 2005 18:56:58 -0600
I want to have tables and outlines where each row can have its own background color. In fact, the background color is an attribute in the entity to which I've bound the table. I can bind the textColor, but this is not what I want.
I've subclassed NSOutlineView and added the following implementation:
- (void)drawBackgroundInClipRect:(NSRect)clipRect
{
NSRange range = [self rowsInRect: clipRect];
int row, count = range.length;
for(row = range.location; count; --count, ++row)
{
NSRect colorRect = NSIntersectionRect([self rectOfRow:row],clipRect);
NSColor* color = [NSColor whiteColor];
NS_DURING
id object = [[self itemAtRow: row] observedObject];
NSData* colorData = [object valueForKey: @"backgroundColor"];
color = [colorData fromBinary];
NS_HANDLER
NS_ENDHANDLER
if(!color) color = [NSColor whiteColor];
NSRectFillListWithColors(&colorRect,&color,1);
}
}
This works reasonably OK except that the whole row doesn't get redrawn when the backgroundColor gets set. Clearly, I need a binding. The question - how to make my own binding and start observing for it in the NSOutlineView.
I've added this goodie:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString: @"backgroundColor"] || [keyPath isEqualToString: @"color"])
{
int row, count = [self numberOfRows];
for(row = 0; row < count; ++row)
{
id rowObject = [[self itemAtRow: row] observedObject];
if(object == rowObject)
{
[self setNeedsDisplayInRect: [self rectOfRow: row]]
}
}
}
}
which should invalidate the rows that need redrawing. However I am uncertain about how to arrange to be an observer on all of the objects in the tree controller for property "backgroundColor". Fiddling about in the debugger isn't a lot of help as it turns out that both the delegate for the table and its dataSource are nil! So where does the data come from?
Trying to do the equivalent in NSTableView is even worse as there is no itemAtRow: method. So I have no idea how to get an object for a given row.
Sure looks like magic to me - where is the controller (from the perspective of the table/outline view and how does the data get into the table/outline?
Todd Blanchard
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden