How do I customize NSOutlineView to have border color?

807 views Asked by At

I have looked over the google but have not found many examples on this. Most only relate to the NSTableView.

How do I subclass my NSOutlineView to look exactly like XCode's plist editor? I've do not know how to get the border colors shown below. I currently have it working exactly the same, except for the coloring. Any help would be appreciated. enter image description here

UPDATE The part I am confused about is what the draw code should look like. Google didnt help me much on this.

1

There are 1 answers

1
Paul Patterson On BEST ANSWER

Step 1: Create an NSTableViewRow subclass

The borders are drawn on the NSTableRowView instances that make up the table's rows. To get them, you could subclass NSTableRowView and give it a borders property that stores a mask specifying which borders you want drawn on the row. You'd accompany this with a custom implementation of drawRect, which calls super first, and then strokes on the specified borders.

Step 2: Implement the delegate methods that signal when you'll need to update the borders

In the simplest case, I can think of three:

- (void)outlineViewItemWillExpand:(NSNotification *)notification
- (void)outlineViewItemDidCollapse:(NSNotification *)notification
- (NSIndexSet *)         outlineView:(NSOutlineView *)outlineView 
selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes

This is where you'll find the hard work. I'd start by having a go at the last one first. There will be lots of different ways of doing it, but you could get the node that's about to be selected from the proposedSelectionIndexes argument in conjunction with the NSOutlineView api. Once you've got this node you can work out which rows will need a border by getting the nodes represented in the rows below this soon-to-be-selected row. If a given node is a descendant of the new selection, then it'll need a border of some sort (hint: the indexPath property of NSTreeNode comes in handy here).

Step 3: Make sure the outline view is using your custom row views

Implement the following delegate method so that it returns your custom row views:

 - (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item

Note that the second item argument is an NSTreeNode object. By keeping a record (a map of some sort) of which nodes are associated with which borders, you can then set the border mask property of your NSTableRowView subclass.