Created
July 28, 2020 13:32
-
-
Save zrzka/b2e98473cceff3d88564503a05c5c4a1 to your computer and use it in GitHub Desktop.
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
// Finder like two levels | |
import Cocoa | |
class ViewController: NSViewController { | |
@IBOutlet var outlineView: NSOutlineView? | |
let data = [ | |
Node(1, "Group A", true, [ | |
Node(11, "A 1"), | |
Node(12, "A 2"), | |
Node(13, "A 3"), | |
]), | |
Node(2, "Group B", true, [ | |
Node(21, "B 1") | |
]) | |
] | |
} | |
extension ViewController: NSOutlineViewDataSource { | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
data.forEach { | |
outlineView?.expandItem($0) | |
} | |
} | |
func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool { | |
!(item as! Node).isGroup | |
} | |
func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any { | |
item == nil ? data[index] : (item as! Node).children[index] | |
} | |
func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool { | |
(item as! Node).isGroup | |
} | |
func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool { | |
(item as! Node).children.count > 0 | |
} | |
func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int { | |
item == nil ? data.count : (item as! Node).children.count | |
} | |
func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? { | |
item | |
} | |
} | |
extension ViewController: NSOutlineViewDelegate { | |
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? { | |
guard let node = item as? Node else { | |
preconditionFailure("Invalid data item \(item)") | |
} | |
let identifier = NSUserInterfaceItemIdentifier(rawValue: node.isGroup ? "HeaderCell" : "DataCell") | |
let view = outlineView.makeView(withIdentifier: identifier, owner: self) as? NSTableCellView | |
view?.textField?.stringValue = node.name | |
if !node.isGroup { | |
view?.imageView?.image = NSImage(systemSymbolName: "folder", accessibilityDescription: nil) | |
} | |
return view | |
} | |
} | |
final class Node { | |
let id: Int | |
let name: String | |
let isGroup: Bool | |
let children: [Node] | |
init(_ id: Int, _ name: String, _ group: Bool = false, _ children: [Node] = []) { | |
self.id = id | |
self.name = name | |
self.isGroup = group | |
self.children = children | |
} | |
} |
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
// Three levels | |
import Cocoa | |
class ViewController: NSViewController { | |
@IBOutlet var outlineView: NSOutlineView? | |
let data = [ | |
Node(1, "Group A", true, [ | |
Node(11, "A 1"), | |
Node(12, "A 2"), | |
Node(13, "A 3"), | |
]), | |
Node(2, "Group B", true, [ | |
Node(21, "B 1", false, [ | |
Node(211, "B 1 1"), | |
Node(212, "B 1 2") | |
]) | |
]) | |
] | |
} | |
extension ViewController: NSOutlineViewDataSource { | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
data.forEach { | |
outlineView?.expandItem($0) | |
} | |
} | |
func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool { | |
!(item as! Node).isGroup | |
} | |
func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any { | |
item == nil ? data[index] : (item as! Node).children[index] | |
} | |
func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool { | |
(item as! Node).isGroup | |
} | |
func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool { | |
(item as! Node).children.count > 0 | |
} | |
func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int { | |
item == nil ? data.count : (item as! Node).children.count | |
} | |
func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? { | |
item | |
} | |
} | |
extension ViewController: NSOutlineViewDelegate { | |
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? { | |
guard let node = item as? Node else { | |
preconditionFailure("Invalid data item \(item)") | |
} | |
let identifier = NSUserInterfaceItemIdentifier(rawValue: node.isGroup ? "HeaderCell" : "DataCell") | |
let view = outlineView.makeView(withIdentifier: identifier, owner: self) as? NSTableCellView | |
view?.textField?.stringValue = node.name | |
if !node.isGroup { | |
view?.imageView?.image = NSImage(systemSymbolName: "folder", accessibilityDescription: nil) | |
} | |
return view | |
} | |
} | |
final class Node { | |
let id: Int | |
let name: String | |
let isGroup: Bool | |
let children: [Node] | |
init(_ id: Int, _ name: String, _ group: Bool = false, _ children: [Node] = []) { | |
self.id = id | |
self.name = name | |
self.isGroup = group | |
self.children = children | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment