Question

I'm new to Cocoa dev, so many concepts of it are not clear to me... I'm trying to build a simple app which will use Flickr API to retrieve user photosets and show them in a NSCollectionView, by clicking them, will start to download the photos of the photo set.

I'm using Xcode 5.0.1 with latest SDK which is 10.9

After reading some articles about how to use binding to deal with NSCollectionView, I'm now facing another problem regarding handling events in NSCollectionViewItem.

Per I understanding, mouse events can be easily handled by implement

-(void) mouseDown:(NSEvent *)theEvent

In a NSView subclass, say

@interface MyViewController : NSView {
}

And assign the view custom class to the subclass I made (MyViewController) in InterfaceBuilder.

Now, I have no problem to do as above, and the mousedown did handled as expect in most of widgets. The problem is, I have a NSCollectionViewItem subclass as below:

@interface MyItemController : NSCollectionViewItem {
}

I'm trying to implement mousedown method there, this class was set to as File's Owner in a separated nib file. And the view will be automatically load when the NSCollectionView loaded.

Now, MyItemController cannot be as customer class in the view object in IB which is obviously because of it is not a NSView subclass but a NSCollectionViewItem subclass.

If I write a subclass of NSView and make the custom class of view object, I can get the mousedown. However, I cannot get the representedObject and index of NSMutableArray in this approach and they are the essential information I need.

So my question is, what is the right way to deal with mouse events view of NSCollectionViewItem? My code in GitHub here:

https://github.com/jasonlu/flickerBackupTool

Thanks!

UPDATE

I found a approach to solve this problem is by subclassing NSView and implement mousedown and use super, subviews to get and index and the array itself

- (void)mouseDown:(NSEvent *)theEvent  {
    NSCollectionView *myCollectionView = (NSCollectionView *)[self superview];
    NSInteger index = [[myCollectionView subviews]  indexOfObject:self];
    NSLog(@"collection view super view: %@",myCollectionView);
    NSLog(@"collection index: %ld",index);
    NSLog(@"array: %@", [[myCollectionView content] objectAtIndex:index]);
}

It seems work, but I'm not sue if this is the best practice, it looks like depends on view too much and took a long way to reach the array.

Was it helpful?

Solution

I wouldn't bet that NSCollectionView always creates all subviews (subviews which are far away from the viewing area might be delayed and/or reused). Therefore, I wouldn't rely upon subview searching.

Overload NSViewController to create an NSView so that the representedObject assigned to the NSViewController is accessible from the NSView. From there you could search the actual content for index determination.

Overloading NSCollectionView and recording the actual index during view creation would probably not work well because a deleted item probably doesNot re-create any views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top