NSOutlineView 的项目是否折叠取决于下面的方法,如果要折叠则返回 true,否则返回 false。
func outlineView(_ outlineView: NSOutlineView, shouldShowOutlineCellForItem item: Any) -> Bool
我尝试制作类似 MAC 系统偏好设置 -> 键盘 -> 快捷方式的演示
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)
}
我的输出如下图: