Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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:
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() {
tableView.dataSource = self
tableView.delegate = self
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
You can’t perform that action at this time.