Swift & NSTableView - just select a row
I would like to programmatically select a row in my NSTableView with one column.
func selectColumnIndexes(_ indexes: NSIndexSet!,
byExtendingSelection extend: Bool)
I've played around with this, but I'm not sure how to write "Select line # 2".
Should I start with a variable associated with @IBOutlet
my view in the table? I have to point out that this is about my NSTableView in some way.
Thank!
+3
source to share
3 answers
Swift 4 solution
I created an extension to make this a bit easier. Also if you have implemented a click action on a cell of the table view this will also call it
extension NSTableView {
func selectRow(at index: Int) {
selectRowIndexes(.init(integer: index), byExtendingSelection: false)
if let action = action {
perform(action)
}
}
}
/// USAGE:
NSTableView().selectRow(at: 0)
+3
source to share