Last active
September 6, 2016 05:11
-
-
Save darknoon/9013525 to your computer and use it in GitHub Desktop.
This is the simplest way I could figure out how to make a view-based NSOutlineView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface AppDelegate : NSObject <NSApplicationDelegate, NSOutlineViewDataSource, NSOutlineViewDelegate> | |
@end | |
@implementation AppDelegate { | |
NSWindow *_w; | |
NSDictionary *_data; | |
} | |
static NSString *const kChildren = @"children"; | |
static NSString *const kTitle = @"title"; | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification | |
{ | |
_data = @{kTitle : @"a", | |
kChildren : @[ | |
@{ | |
kTitle: @"b", | |
kChildren : @[@{kTitle: @"c"}, @{kTitle: @"d"}, @{kTitle: @"e"}] | |
}, | |
@{ | |
kTitle: @"f", | |
kChildren : @[@{kTitle: @"g"}, @{kTitle: @"h"}] | |
} | |
] | |
}; | |
CGRect contentRect = CGRectMake(100, 100, 500, 400); | |
_w = [[NSWindow alloc] initWithContentRect:contentRect | |
styleMask:NSTitledWindowMask|NSClosableWindowMask | |
backing:NSBackingStoreBuffered | |
defer:NO]; | |
CGRect interior = (CGRect){.size = contentRect.size}; | |
NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:interior]; | |
NSOutlineView *outlineView = [[NSOutlineView alloc] initWithFrame:interior]; | |
outlineView.dataSource = self; | |
outlineView.delegate = self; | |
NSTableColumn *tc = [[NSTableColumn alloc] initWithIdentifier:@"blah"]; | |
[outlineView addTableColumn:tc]; | |
tc.width = interior.size.width; | |
[outlineView setOutlineTableColumn:tc]; | |
scrollView.documentView = outlineView; | |
_w.contentView = scrollView; | |
[_w makeKeyAndOrderFront:nil]; | |
} | |
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(NSDictionary *)item | |
{ | |
return item ? [item[kChildren] count] : 1; | |
} | |
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item | |
{ | |
return item ? [item[kChildren] objectAtIndex:index] : _data; | |
} | |
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item | |
{ | |
return [item[kChildren] count] > 0; | |
} | |
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item | |
{ | |
NSTextField *t = [[NSTextField alloc] initWithFrame:CGRectZero]; | |
t.editable = NO; | |
t.bordered = NO; | |
t.backgroundColor = [NSColor clearColor]; | |
t.stringValue = item[kTitle]; | |
return t; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment