Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How to find selected item in NSOutlineView?

How to find selected item in NSOutlineView?
Thread Tools
DSHwrd
Junior Member
Join Date: May 2002
Location: Okinawa, Japan (Kadena AB)
Status: Offline
Reply With Quote
Aug 5, 2003, 11:30 AM
 
Maybe I'm just missing something obvious here, but is there an easy way to find the currently selected item in an OutlineView? Before someone gets all smart and says 'Yea, just use [outlineView selectedRow];.' Let me say that that doesn't give me the desired results. In my outline view, there are several expandable items within expandable items. So finding the currently selected item is kind of a pain. I haven't found a way to do it yet, and Apple's DragNDropOutlineView is confusing as all heck (I hate their Macro usage, makes everything confusing -- especially for newbies). Let me explain a little more. When this method fires:

- (BOOL)selectionShouldChangeInOutlineViewNSOutlin eView *)outlineView

I need to be able to find what item is selected, and then save some information based on that item.

Anyone know of a way to do this? I've been trying to figure it out for a few months now and I'm finaly at the stage of begging people to help me figure this out.

Thanks in advance,
- Daniel
Daniel Howard | Mac OS X (10.2.2) | TiBook867 /\ iSub w/ Soundsticks /\ iPod
www.midnite-liteman.com | ideaSpiral 1.5.3 / DVD Rack 1.0.2
     
calumr
Forum Regular
Join Date: Sep 2000
Location: UK
Status: Offline
Reply With Quote
Aug 5, 2003, 11:54 AM
 
Code:
- (id)itemAtRow:(int)row
Just pass in the selectedRow, and you're done!
     
DSHwrd  (op)
Junior Member
Join Date: May 2002
Location: Okinawa, Japan (Kadena AB)
Status: Offline
Reply With Quote
Aug 5, 2003, 03:44 PM
 
OMG, you know that makes complete sense. I've been looking at itemForRow for sometime now, but it never clicked as to how it would work. It just clicked! Thanks!

Cheers,
- Daniel
Daniel Howard | Mac OS X (10.2.2) | TiBook867 /\ iSub w/ Soundsticks /\ iPod
www.midnite-liteman.com | ideaSpiral 1.5.3 / DVD Rack 1.0.2
     
DSHwrd  (op)
Junior Member
Join Date: May 2002
Location: Okinawa, Japan (Kadena AB)
Status: Offline
Reply With Quote
Aug 5, 2003, 04:09 PM
 
Ok, I lied... I tested it out and it didn't work. *grins*

This is my code. Maybe you can see something that is making it not work?

//This method is called right before a change is made in the outlineView.
- (BOOL)selectionShouldChangeInOutlineViewNSOutlin eView *)outlineView
{
NSData *data = [textField RTFDFromRange:NSMakeRange(0, [[textField string] length])];
[[noteData objectAtIndex:[noteBoxView itemAtRow:[noteBoxView selectedRow]]] setObject:data forKey:@"Notes"];
NSLog(@"...selectionShouldChangeInOutlineView");
}

Thanks in advance,
- Daniel
Daniel Howard | Mac OS X (10.2.2) | TiBook867 /\ iSub w/ Soundsticks /\ iPod
www.midnite-liteman.com | ideaSpiral 1.5.3 / DVD Rack 1.0.2
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 5, 2003, 05:31 PM
 
It looks like objectAtIndex: should take an int, but you're giving it an id (from itemAtRow:).
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
MaxPower2k3
Mac Elite
Join Date: Jan 2003
Location: NYC
Status: Offline
Reply With Quote
Aug 5, 2003, 05:45 PM
 
yeah, objectAtIndex needs an int, and selectedRow returns an int, so you jsut need to do...

[noteData objectAtIndex:[noteBoxView selectedRow]]
     
Jan Van Boghout
Registered User
Join Date: Sep 2001
Status: Offline
Reply With Quote
Aug 5, 2003, 08:29 PM
 
Yup, just change it to [[noteBoxView itemAtRow:[noteBoxView selectedRow]] setObject:data forKey:@"Notes"];
     
DSHwrd  (op)
Junior Member
Join Date: May 2002
Location: Okinawa, Japan (Kadena AB)
Status: Offline
Reply With Quote
Aug 5, 2003, 09:34 PM
 
Originally posted by Jan Van Boghout:
Yup, just change it to [[noteBoxView itemAtRow:[noteBoxView selectedRow]] setObject:data forKey:@"Notes"];

Thanks for the reply, but I do have one question. If I use the above code, how would my array 'noteData' know to add the desired data forKey:@"Notes"? I'm a bit confused...

Thanks,
- Daniel
Daniel Howard | Mac OS X (10.2.2) | TiBook867 /\ iSub w/ Soundsticks /\ iPod
www.midnite-liteman.com | ideaSpiral 1.5.3 / DVD Rack 1.0.2
     
Jan Van Boghout
Registered User
Join Date: Sep 2001
Status: Offline
Reply With Quote
Aug 6, 2003, 06:40 AM
 
Originally posted by DSHwrd:
Thanks for the reply, but I do have one question. If I use the above code, how would my array 'noteData' know to add the desired data forKey:@"Notes"? I'm a bit confused...

Thanks,
- Daniel
I assume the object that the selected item represents listens to setObject:forKey: ?

For example, in my app I have a collection of CSSObject classes to which I can apply some values such as setName: , setSource: etc. Some of the objects can be groups etc etc. Getting the selected item, I can just call setName: after an edit for example, without having to search what item is clicked myself.


Hmm� while writing this I am confused How does your underlying model for the outline view work? A main array that contains normal objects and other arrays? Because setObject:forKey is normally a method for dictionaries, unless you make your own version for a custom class.
     
DSHwrd  (op)
Junior Member
Join Date: May 2002
Location: Okinawa, Japan (Kadena AB)
Status: Offline
Reply With Quote
Aug 6, 2003, 01:54 PM
 
Yes, it is a dictionary. Heh. I figured it'd be easier to do it that way. I think I'm kind of understanding what you're saying. Just use the datasource to add my information too. (It makes sense in my head... heh) I'll give it a shot and post back here on whether or not it works. I'm hopeful!

Thanks,
- Daniel
Daniel Howard | Mac OS X (10.2.2) | TiBook867 /\ iSub w/ Soundsticks /\ iPod
www.midnite-liteman.com | ideaSpiral 1.5.3 / DVD Rack 1.0.2
     
DSHwrd  (op)
Junior Member
Join Date: May 2002
Location: Okinawa, Japan (Kadena AB)
Status: Offline
Reply With Quote
Aug 6, 2003, 02:27 PM
 
yay! I got it to work. Now I need to work on Drag and Drop... Yippie... Oh the joy.

Thanks for all the help,
- Daniel
Daniel Howard | Mac OS X (10.2.2) | TiBook867 /\ iSub w/ Soundsticks /\ iPod
www.midnite-liteman.com | ideaSpiral 1.5.3 / DVD Rack 1.0.2
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:26 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,