View Full Version : NSCollectionView - selecting items?

Mar 3, 2013, 02:11 PM
I have been working on my Mac app on and off. I use it to help me produce images for an on screen weekly specials for my local grocery store. This weekend I tried something new to help speed things up. Instead of searching the database of items I created and NSCollectionView. My hopes are click on images and have them populate to the lefts side (in picture).

I got the NSCollectionView working but I am unable to select items in this view. Instead when I click on something the whole NSScrollView gets selected?

Is the NSCollectionView the correct object to use for this? I was also looking at the IKImageBrowserView, is that better for interactivity with objects in a view like the CollectionView?

Red Menace
Mar 3, 2013, 05:34 PM
Is the view template set to be selectable?

Mar 3, 2013, 10:14 PM
Is the view template set to be selectable?


[collectionView setSelectable:YES];

I tried a different approach too and added a NSButton to each one. But this is also my first project dealing with "Bindings" and it has been a struggle adding targets to binding.

Here is the code that sets up the array for the NSCollectionView

_itemDatabaseArray = [[NSMutableArray alloc]init];
int countForIndex = 0; // use for tag.

NSFileManager *fm = [NSFileManager defaultManager];
NSString *pathName = @"/Users/larspro/Documents/ERad files/items database";

NSURL *directoryURL = [[NSURL alloc] initFileURLWithPath:pathName];
NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey];
// Below will enumerates through a directory
NSDirectoryEnumerator *enumerator = [fm enumeratorAtURL:directoryURL includingPropertiesForKeys:keys
options:0 errorHandler:^(NSURL *url, NSError *error) {
// Handle the error.
// Return YES if the enumeration should continue after the error.
return YES;
for (NSURL *url in enumerator) {

NSString *theString = [NSString stringWithFormat:@"%@", url]; // convert the URL to NSString to test
NSArray *splitStringArray = [theString componentsSeparatedByString:@"."]; // Splits the string looking for 'plist'
NSString *result = [splitStringArray lastObject];

if ([result isEqualToString:@"plist"]) {
NSError *error;
NSNumber *isDirectory = nil;
if (! [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error]) {
// handle error
else if (! [isDirectory boolValue]) {
NSMutableArray *items = [[NSMutableArray alloc] initWithContentsOfURL:url];
dataBaseItem *newItem = [[dataBaseItem alloc]init];

NSImage *importImage = [[NSImage alloc] initWithData:[items objectAtIndex:0]];

newItem.itemName = [items objectAtIndex:1];
newItem.itemImage = importImage;
newItem.selectButton.tag = countForIndex;
[newItem.selectButton setTarget:self];
[newItem.selectButton setAction: @selector(itemSelected)];

[arrayController addObject:newItem];
NSLog(@"count:%ld", (unsigned long)_itemDatabaseArray.count);


Mar 4, 2013, 07:35 PM
Could not figure this out so I built my own NSScrollView and populated it with NSButton's with images and text on the buttons.

Looks the same as the NSCollectionView but now it's selectable and I have targets for the buttons.