开发者

SelectionIndexes on NSArrayController return just a value

开发者 https://www.devze.com 2023-03-17 06:10 出处:网络
I have a NSCollectionView wich content is handled by a NSArrayController. The NSCollectionView is selectable and I need to retrieve a list of selected elements.

I have a NSCollectionView wich content is handled by a NSArrayController. The NSCollectionView is selectable and I need to retrieve a list of selected elements. I'm trying to observe key property of NSArrayController "selectionIndexes" but it just return me ALWAYS the value of the first element in CollectionView and not the selected items.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualTo:@"selectionIndexes"])
    {
        //True if in开发者_如何学JAVA the array controller of the collection view really exists at least a selected object
        if([[arrayController selectedObjects] count] > 0)
        {
            NSLog(@"Selected objects: %@", [arrayController selectedObjects]);
        }
        else
        {
            NSLog(@"Observer called but no objects where selected.");
        }
    }
}

UPDATE

I never get this method called, if I manually invoke NSLog(@"Selected objects: %@", [arrayController selectedObjects]) I get this

The result is always something like this

END UPDATE

2011-07-05 20:44:45.711 collectionView2[2153:903] Selected objects 1: (
    "<Hormiga: 0x10013e330>"
)

I think I have done something wrong binding NSArrayController with NSCollectionView. What could be my fault? Tell me if you want more info, I can even post the entire program in a zip if you need it.

UPDATE 2

This is the code I use in my controller to observe the arrayController "selectionIndexes" key.

[arrayController addObserver:self forKeyPath:@"selectionIndexes" options:NSKeyValueObservingOptionNew context:nil];

UPDATE 3

One of the problem was fixed, I forgot to set the binding between NSArrayController and NSCollectionView relative to the key "selectionIndexes". Now I can retrieve manually the list of selectedObject and its correct!

My final problem is that I don't receive the notification when selectionIndexes changes. So observeValueForKeyPath:ofObject:change:context: never get called!

UPDATE 4

I was trying to set the observer in the init method of my controller, but in this way the arrayController is still null. Moving the addObserver in the awakeForNib resolved all my problems!


If you want to keep the array controller’s selection indexes in sync with the collection view’s, you need to bind them as well. In summary:

  • Bind the collection view Content to the array controller, key arrangedObjects
  • Bind the collection view Selection Indexes to the array controller, key selectionIndexes.

Also, make sure arrayController has been set before you add an observer. Outlets are guaranteed to be set in -awakeFromNib and other methods that are invoked after it: If you’re using a window controller, you can use -windowDidLoad; if you’re using a view controller, you can use -loadView; otherwise, -applicationDidFinishLaunching: in your application delegate.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号