Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Programmatically create NSTableView in Swift 4.1
import Cocoa
class SidebarViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
var initialized = false
let scrollView = NSScrollView()
let tableView = NSTableView()
override func loadView() {
self.view = NSView()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayout() {
if !initialized {
initialized = true
setupView()
setupTableView()
}
}
func setupView() {
self.view.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200))
}
func setupTableView() {
self.view.addSubview(scrollView)
self.scrollView.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1.0, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 23))
self.view.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1.0, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1.0, constant: 0))
tableView.frame = scrollView.bounds
tableView.delegate = self
tableView.dataSource = self
tableView.headerView = nil
scrollView.backgroundColor = NSColor.clear
scrollView.drawsBackground = false
tableView.backgroundColor = NSColor.clear
tableView.appearance = NSAppearance(named: NSAppearance.Name.vibrantDark)
let col = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "col"))
col.minWidth = 200
tableView.addTableColumn(col)
scrollView.documentView = tableView
scrollView.hasHorizontalScroller = false
scrollView.hasVerticalScroller = true
}
func numberOfRows(in tableView: NSTableView) -> Int {
return 100
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let text = NSTextField()
text.stringValue = "Hello World"
let cell = NSTableCellView()
cell.addSubview(text)
text.drawsBackground = false
text.isBordered = false
text.translatesAutoresizingMaskIntoConstraints = false
cell.addConstraint(NSLayoutConstraint(item: text, attribute: .centerY, relatedBy: .equal, toItem: cell, attribute: .centerY, multiplier: 1, constant: 0))
cell.addConstraint(NSLayoutConstraint(item: text, attribute: .left, relatedBy: .equal, toItem: cell, attribute: .left, multiplier: 1, constant: 13))
cell.addConstraint(NSLayoutConstraint(item: text, attribute: .right, relatedBy: .equal, toItem: cell, attribute: .right, multiplier: 1, constant: -13))
return cell
}
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
let rowView = NSTableRowView()
rowView.isEmphasized = false
return rowView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment