I want to Select A Row in my table view programmatically, I believe I would use selectRowIndexes:byExtendingSelection:
(is this a delegate method?). The other thing is how would I use that method to select the second row (in programming terms row 1)?
Asked
Active
Viewed 2.8k times
33
1 Answers
96
Joshua, make sure to use the developers documentation to determine whether or not it's a delegate method. If it were a delegate method, it would be mentioned in the docs for NSTableViewDelegate.
What you’re looking for is very straight forward.
Objective-C
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1];
[tableview selectRowIndexes:indexSet byExtendingSelection:NO];
Swift 2
let indexSet = NSIndexSet(index: 1)
tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
Again. Make sure to look up the method selectRowIndexes:byExtendingSelection in the docs to see what parameters it needs. It says an NSIndexSet is needed. Then look up NSIndexSet and you'll discover how to use that.

Brad G
- 2,528
- 1
- 22
- 23
-
This doesn't seem to work. I'm using this with an Outline View but it should still work, Right? Because NSOutlineView is a Sub-Class of NSTableView? – Joshua Dec 18 '09 at 16:21
-
2This should still work. Make sure that your outlets are connected and make sure your calling this method after the view has been initialized. Hence, do not call this in an "init" method, use - (void)awakeFromNib. – Brad G Dec 18 '09 at 16:46
-
Odd, It doesn't work in -(void)awakeFromNib but it does work as an IBAction linked to a button. – Joshua Dec 18 '09 at 17:49
-
is the view loaded from a nib? or are you programmatically creating it? – Brad G Dec 18 '09 at 18:08
-
The View is a window in the nib. – Joshua Dec 18 '09 at 18:28
-
how is the method that invokes the code provided called? If invoking the code by a user action works then you must be calling it when the view is not yet loaded from the nib. The controller that invokes the code may not be loaded from a nib, so awakeFromNib may never be called. – Brad G Dec 18 '09 at 21:17
-
The controller is an object in the nib. Could it be that the Outline View is not yet populated when awakeFromNib is called? – Joshua Dec 19 '09 at 07:51
-
When else/In what other method could the Outline View be populated? – Joshua Dec 19 '09 at 16:29
-
If it makes any difference I am trying to do this with the NewsReader example project. http://developer.apple.com/mac/library/samplecode/NewsReader/index.html – Joshua Dec 19 '09 at 16:42
-
Don't worry I've figured it out! – Joshua Dec 19 '09 at 16:47
-
Yep it works. Thanks. They didn't make this obvious though. I've looked around a lot for this solution. – Miek Jun 05 '12 at 20:07
-
**Swift 2.0:** tableView.selectRowIndexes(NSIndexSet(index: 1), byExtendingSelection: false) – Megaetron Mar 21 '16 at 19:42
-
I wish it would *not* be calling the delegate method. (Just like iOS UITableView does not call delegate method when programatically selecting the row.) – Sunil Chauhan Jun 20 '17 at 18:19
-
@Joshua it will work in awakeFromNib if you wrap it in DispatchQueue.main.async – Gamec Sep 13 '18 at 10:23
-
1Now use: `IndexSet(integer: 1)` – Sentry.co May 24 '21 at 12:18
-
Also note that if you programmatically select a cell, you will have to set isEmphasized to true in a custom NSTableRowView – Sentry.co May 24 '21 at 12:27