Skip to content

Instantly share code, notes, and snippets.

@helje5
Created December 27, 2025 11:58
Show Gist options
  • Select an option

  • Save helje5/4836c1759a8bbe84ac914d2ded90aa3a to your computer and use it in GitHub Desktop.

Select an option

Save helje5/4836c1759a8bbe84ac914d2ded90aa3a to your computer and use it in GitHub Desktop.
One Column NSTableView
public extension ZzViewFactory {
func makeOneColumnTableView(_ configure: ( UXTableView ) -> Void) -> UXView {
return makeTableView { tv in
#if os(macOS)
tv.headerView = nil // no header view
tv.allowsColumnResizing = false
tv.allowsColumnSelection = false
tv.allowsEmptySelection = true
tv.allowsMultipleSelection = false
let tcID = NSUserInterfaceItemIdentifier("result")
let tc = NSTableColumn(identifier: tcID)
tc.isEditable = false
tc.resizingMask = .autoresizingMask
tv.addTableColumn(tc)
// seems to be necessary to make the column expand the whole width
tv.sizeLastColumnToFit()
#else
tv.allowsMultipleSelection = false
#endif
configure(tv)
}
}
}
@helje5
Copy link
Author

helje5 commented Dec 27, 2025

Related:

  /// CAREFUL: Does not set translatesAutoresizingMaskIntoConstraints=false
  open func makeScrollView(_ nestedView: UXView) -> UXScrollView {
    let sv = ZzExtendedScrollView(frame: .initialFrame)
    // Note: translatesAutoresizingMaskIntoConstraints is ON!
    // FIXME: HH: Yes, but why?

    #if false // nestedView should be flipped!
    nestedView.translatesAutoresizingMaskIntoConstraints = false
    nestedView.frame = sv.contentView.bounds
    #endif

    sv.usesPredominantAxisScrolling = false // YES by default
    sv.autohidesScrollers           = true // default NO
    sv.hasHorizontalScroller        = true
    sv.hasVerticalScroller          = true
    sv.documentView                 = nestedView

    #if false // nestedView should be flipped!
    // Apple Docs say to do this to disable horiz scrolling:
    // To disable horizontal scrolling, set the content view’s
    // width equal to the scroll view’s width
    [ constrain(nestedView, .leading,  to: sv.contentView),
      constrain(nestedView, .trailing, to: sv.contentView),
      constrain(nestedView, nestedView.isFlipped ? .top : .bottom,
                to: sv.contentView)
    ].activate()
    #endif
    
    return sv
  }

  /// CAREFUL: Does not set translatesAutoresizingMaskIntoConstraints=false
  open func makeTableView() -> UXView {
    // embed in scroll view, should always be done for tableviews.
    // Note: translatesAutoresizingMaskIntoConstraints is ON!
    // FIXME: HH: Yes, but why? (I guess when it is used as a VC root view)
    let tv = ZzExtendedTableView(frame: .initialFrame)
    tv.autoresizingMask = [ .width, .height ]
    return makeScrollView(tv)
  }
  
  /// CAREFUL: Does not set translatesAutoresizingMaskIntoConstraints=false
  open func makeTableView(_ block: ( NSTableView ) -> Void) -> UXView {
    let sv = makeTableView() as! NSScrollView
    block(sv.documentView as! NSTableView)
    return sv
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment