views:

104

answers:

1

I want to Select A Row in my table view programatically, 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)?

+3  A: 

Joshua, make sure to use the developers documentation to determine weather or not it's a delegate method. If it were a delegate method, it would be mentioned under the NSTableViewDelegate docs.

What your looking for is very straight forward.

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1];
[tableview selectRowIndexes:indexSet byExtendingSelection:NO];

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 lookup NSIndexSet and you'll discover how to use that.

Brad Goss
Ok, I see. Thanks for your kind help.
Joshua
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
This 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 Goss
Odd, It doesn't work in -(void)awakeFromNib but it does work as an IBAction linked to a button.
Joshua
is the view loaded from a nib? or are you programmatically creating it?
Brad Goss
The View is a window in the nib.
Joshua
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 Goss
The controller is an object in the nib. Could it be that the Outline View is not yet populated when awakeFromNib is called?
Joshua
Yes. Very possible.
Brad Goss
When else/In what other method could the Outline View be populated?
Joshua
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
Don't worry I've figured it out!
Joshua