Created
July 31, 2020 10:28
-
-
Save waynezhang/c8e0b42c809c51a984401c890399989f to your computer and use it in GitHub Desktop.
OutlineView Example
This file contains hidden or 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
import Cocoa | |
class ViewController: NSViewController { | |
internal lazy var outlineView: NSOutlineView = { | |
let outlineView = NSOutlineView() | |
outlineView.usesAutomaticRowHeights = true | |
let column = NSTableColumn(identifier: .init(rawValue: "TestColumn")) | |
outlineView.addTableColumn(column) | |
outlineView.outlineTableColumn = column | |
outlineView.headerView = nil | |
outlineView.backgroundColor = .clear | |
outlineView.indentationPerLevel = 16 | |
outlineView.allowsEmptySelection = false | |
outlineView.refusesFirstResponder = true | |
return outlineView | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let scrollView = NSScrollView() | |
scrollView.translatesAutoresizingMaskIntoConstraints = false | |
scrollView.drawsBackground = false | |
view.addSubview(scrollView) | |
NSLayoutConstraint.activate([ | |
scrollView.topAnchor.constraint(equalTo: view.topAnchor), | |
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor), | |
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor), | |
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor), | |
]) | |
scrollView.documentView = outlineView | |
outlineView.dataSource = self | |
outlineView.delegate = self | |
} | |
override var representedObject: Any? { | |
didSet { | |
// Update the view, if already loaded. | |
} | |
} | |
} | |
extension ViewController: NSOutlineViewDataSource { | |
func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int { | |
item == nil ? 10 : 0 | |
} | |
func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool { | |
false | |
} | |
func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any { | |
"Item \(index)" | |
} | |
} | |
extension ViewController: NSOutlineViewDelegate { | |
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? { | |
var cellView = outlineView.makeView(withIdentifier: .init(rawValue: "CellView"), owner: nil) as? CellView | |
if cellView == nil { | |
cellView = CellView(frame: .zero) | |
cellView?.identifier = .init("CellView") | |
cellView?.textField?.isEditable = true | |
cellView?.textField?.stringValue = (item as? String) ?? "" | |
} | |
return cellView | |
} | |
} | |
class CellView: NSTableCellView { | |
override init(frame frameRect: NSRect) { | |
super.init(frame: frameRect) | |
let textField = NSTextField() | |
textField.translatesAutoresizingMaskIntoConstraints = false | |
textField.font = .controlContentFont(ofSize: 14) | |
textField.textColor = .controlTextColor | |
textField.isBordered = false | |
textField.backgroundColor = .clear | |
textField.focusRingType = .none | |
textField.usesSingleLineMode = true | |
addSubview(textField) | |
NSLayoutConstraint.activate([ | |
textField.topAnchor.constraint(equalTo: topAnchor), | |
textField.bottomAnchor.constraint(equalTo: bottomAnchor), | |
textField.leadingAnchor.constraint(equalTo: leadingAnchor), | |
textField.trailingAnchor.constraint(equalTo: trailingAnchor), | |
]) | |
self.textField = textField | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment