Last active
July 28, 2022 02:30
-
-
Save christopherjohst/a80bec25661b985118a55ca33ecfdff2 to your computer and use it in GitHub Desktop.
A complete example showing how to construct an NSTableView programmatically (no NIB!) and take care of the small issues like column spacing. Complete description of this example here: https://kitcross.net/nstableview-in-playgrounds/
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
import AppKit | |
import PlaygroundSupport | |
let tableViewFrame = NSRect(x: 0, y: 0, width: 250, height: 500) | |
class TableViewController: NSViewController { | |
let tableView: NSTableView = { | |
let tableView = NSTableView() | |
// By default a tableview has column spacing. We only have | |
// one column so this will the table from side scrolling | |
tableView.intercellSpacing = NSSize(width: 0, height: 2) | |
tableView.headerView = nil | |
return tableView | |
}() | |
let scrollView = NSScrollView() | |
let column = NSTableColumn() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView.dataSource = self | |
tableView.delegate = self | |
tableView.addTableColumn(column) | |
column.width = tableViewFrame.size.width | |
scrollView.documentView = tableView | |
} | |
// Loading a view controller without a NIB requires that we | |
// override loadView and provide our own view instead | |
override func loadView() { | |
view = scrollView | |
} | |
} | |
extension TableViewController: NSTableViewDataSource { | |
func numberOfRows(in tableView: NSTableView) -> Int { | |
return 30 | |
} | |
} | |
extension TableViewController: NSTableViewDelegate { | |
func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { | |
return 20 | |
} | |
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { | |
return NSTextField(labelWithString: "Cell \(row + 1)") | |
} | |
} | |
let vc = TableViewController(nibName: nil, bundle: nil) | |
vc.view.frame = tableViewFrame | |
PlaygroundPage.current.liveView = vc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment