Item of NSOutlineView will collapse or not depends upon below method, return true if you want to collapse otherwise false.
func outlineView(_ outlineView: NSOutlineView, shouldShowOutlineCellForItem item: Any) -> Bool
I tried to make same a demo like MAC system preference -> keyboard -> Shortcuts
enter image description here
func outlineView(_ outlineView: NSOutlineView, shouldShowOutlineCellForItem item: Any) -> Bool {
let isExpanded = outlineView.isItemExpanded(item)
if isExpanded {
return true
} else {
return false
}
}
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
let view = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "OutlineTableCell"), owner: self) as! OutlineTableCell
view.setOutlineTableCellLaytout(forNode: (item as? SectionTreeNode)!)
view.disclosure.action = nil
view.disclosure.action = #selector(didDisclosureClicked(_:))
view.selectedItem = item as? SectionTreeNode
return view
}
@objc func didDisclosureClicked(_ sender: NSButton) {
let view = sender.superview?.superview as? OutlineTableCell
let isExpand = outlineView.isItemExpanded(view?.selectedItem)
isExpand ? outlineView.collapseItem(view?.selectedItem, collapseChildren: true) : outlineView.expandItem(view?.selectedItem, expandChildren: true)
}
My output is below image:
enter image description here