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!
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)
@ Answer to Isaiah in Swift 3.0:
.selectRowIndexes(NSIndexSet(index: 0) as IndexSet, byExtendingSelection: false)
Ok, I did it. It turned out
@IBOutlet weak var NoteTableView: NSTableView!
NoteTableView.selectRowIndexes(NSIndexSet(index: 0), byExtendingSelection: false)
I could not fully define part c NSIndexSet
. I fiddled with init()
what turned out to be unnecessary.