Re: Selection in NSCollectionView
Re: Selection in NSCollectionView
- Subject: Re: Selection in NSCollectionView
- From: Markus Spoettl <email@hidden>
- Date: Wed, 3 Sep 2008 23:32:49 -0700
On Sep 3, 2008, at 5:19 PM, Quincey Morris wrote:
But even after I tried to do something similar, I never got the
collection to select things using the mouse, so I gave up. (It does
work in IconCollection, but I couldn't find out why, or what was
different.) That was the 3rd time I gave up trying to use
NSCollectionView for something or other.
I don't know about how to make this work with compound views - by that
I mean views containing other standard Cocoa subviews. I can imagine
things get considerably more difficult because those sub views all
have their own event handling implemented (mouse, keyboard). I also
failed to make this work in my early tests, if I remember correctly
the hitTest thin never worked (I could be wrong - it was several
months back).
However, I have used NSCollectionView in conjunction with custom
NSViews and selection is no problem. Basically there are two ways a
view can get selected:
1) The selection state is change through the view's item controller.
This usually happens when the selection indexes of the bound
NSArrayController that controls the collection view's content change.
The view has to be informed by its controller that it has been
selected so it can update its visual state accordingly. This can be
done by overwriting setSelected in the view's item controller.
2) An event in the view takes place that requires it to become
selected or de-selected. This can be something like a -mouseDown:
event, a keyboard event (if the view is can become and is
firstResponder), probably not much else. In that case, the view
informs its item controller (every view has its own controller) that
it has been selected (or de-selected). The item controller forwards
the information change to the collection view. After than (1) is what
happens.
For this to work you need to implement a custom NSCollectionViewItem
subclass that overwrites -setSelected:. In there you first call super
and then let the view know about the selection state (probably by
giving the view its own -selected property). You will also need to
overwrite -setRepresentedObject: and let the view know who its
controller is (and possibly the represented object too):
- (void)setSelected:(BOOL)flag {
[super setSelected:flag];
// tell the view that it has been selected
[(MyCustomView* )[self view] setSelected:flag];
}
- (void)setRepresentedObject:(id)object
{
[super setRepresentedObject:object];
MyCustomView *view = (MyCustomView *)[self view];
[view setController:self];
[view setRepresentedObject:object];
}
After that your NSView subclass knows both its controller object and
the object it represents and it can forward events of interest to the
item controller.
Not sure if that brings you any closer to a solution for your problem.
Markus
--
__________________________________________
Markus Spoettl
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden