Skip to content

Instantly share code, notes, and snippets.

@jasonm23
Last active May 10, 2018 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonm23/387d07f5be65bf53e862467c17f1c6de to your computer and use it in GitHub Desktop.
Save jasonm23/387d07f5be65bf53e862467c17f1c6de to your computer and use it in GitHub Desktop.
Generic function to get any class of NSView for a NSTableView
import Cocoa
// Use this as a helper function in your table view delegate.
func getRowView<T: NSView>(_ tableView: NSTableView) -> T {
let identifier = NSUserInterfaceItemIdentifier(
rawValue: "\(T.self)")
var dequeuedRowView: T? = tableView.makeView(
withIdentifier: identifier, owner: self
) as? T
if dequeuedRowView == nil {
dequeuedRowView = T.fromNib()
dequeuedRowView?.identifier = identifier
}
guard let rowView: T = dequeuedRowView
else { fatalError("Unable to get a \(T.self)") }
return rowView
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment