Last active
May 10, 2018 11:50
-
-
Save jasonm23/387d07f5be65bf53e862467c17f1c6de to your computer and use it in GitHub Desktop.
Generic function to get any class of NSView for a NSTableView
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 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