View-based NSOutlineView without NIB?

后端 未结 3 1570
难免孤独
难免孤独 2020-12-31 21:44

NSOutlineView is a subclass of NSTableView. And currently, NSTableView supports two implementations.

  • Cell-based.
3条回答
  •  余生分开走
    2020-12-31 22:01

    If you follow the example in SidebarDemo, they use a subclass of NSTableCellView for the detail rows. In order to emulate the InterfaceBuilder mojo, you can hook everything together in the constructor. The rest is the same as the demo (see outlineView:viewForTableColumn:item:).

    @interface SCTableCellView : NSTableCellView
    @end
    
    @implementation SCTableCellView
    
    - (id)initWithFrame:(NSRect)frameRect {
      self = [super initWithFrame:frameRect];
      [self setAutoresizingMask:NSViewWidthSizable];
      NSImageView* iv = [[NSImageView alloc] initWithFrame:NSMakeRect(0, 6, 16, 16)];
      NSTextField* tf = [[NSTextField alloc] initWithFrame:NSMakeRect(21, 6, 200, 14)];
      NSButton* btn = [[NSButton alloc] initWithFrame:NSMakeRect(0, 3, 16, 16)];
      [iv setImageScaling:NSImageScaleProportionallyUpOrDown];
      [iv setImageAlignment:NSImageAlignCenter];
      [tf setBordered:NO];
      [tf setDrawsBackground:NO];
      [[btn cell] setControlSize:NSSmallControlSize];
      [[btn cell] setBezelStyle:NSInlineBezelStyle];
      [[btn cell] setButtonType:NSMomentaryPushInButton];
      [[btn cell] setFont:[NSFont boldSystemFontOfSize:10]];
      [[btn cell] setAlignment:NSCenterTextAlignment];
      [self setImageView:iv];
      [self setTextField:tf];
      [self addSubview:iv];
      [self addSubview:tf];
      [self addSubview:btn];
      return self;
    }
    
    - (NSButton*)button {
      return [[self subviews] objectAtIndex:2];
    }
    
    - (void)viewWillDraw {
      [super viewWillDraw];
      NSButton* btn = [self button];
      ...
    

提交回复
热议问题