Artem Novichkov
Artem Novichkov

Reputation: 2341

NSCollectionView multiple selection like in iOS

I'm working with NSCollectionView and want to add multiple selection. I checked Allows Multiple Selection in xib, but in works only with selection by Select+Drag action. I want to implement selection by single tap on every collection view item. How I can do that? I thought about saving of selected state into model directly, but I guess there is more elegant approach.

Edited:

I discovered that multiple selection works with pressed Shift. Anyway, I want to present selection by dragging.

Upvotes: 0

Views: 843

Answers (1)

Yogesh Arora
Yogesh Arora

Reputation: 19

Although I didn't implement any sample on collection view but still there is some idea in my mind as per your query.

We can create custom subclass of collection view item and place one background view which shows the selection using background color.

Whenever the selection change in collection view then we can get the delegate call using delegate method such as

  • (void)collectionView:(NSCollectionView *)collectionView didSelectItemsAtIndexPaths:(NSSet *)indexPaths

In this method we can check whether item is already selected or not using some bool property on custom collection item. If item already selected then we can set the clear background color of collection item background view otherwise can set the selection color. And internally we can manage an array of indexes of selected items.

I hope it will give you some help.

I have implemented one sample for showing multiple selection creating subclass of Collection Item.

In this sample I have created custom collection view item. There is one border view which contain button and hit of button I am changing the border view color to appear like this item is selected. We can create one more item property like index number. And you can create protocol inside this subclass and call the delegate object to store the selected item index to perform some operation over it further.

AppDelegate.h

#import <Cocoa/Cocoa.h>

@class CustomCollectionItem;

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (strong) CustomCollectionItem *collectionViewItem;
@property (strong) NSArray *contents;
@property (nonatomic, weak) IBOutlet NSCollectionView *collectionView;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "CustomCollectionItem.h"


@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self populateCollectionView];
}

- (void)populateCollectionView
{
    self.collectionViewItem = [[CustomCollectionItem alloc] init];

    self.contents = @[
                      @{@"itemTitle":@"Item 1",
                        @"isSelected":@NO,
                       },

                      @{@"itemTitle":@"Item 2",
                        @"isSelected":@NO,
                       },

                      @{@"itemTitle":@"Item 3",
                        @"isSelected":@NO,
                       },

                      @{@"itemTitle":@"Item 4",
                        @"isSelected":@NO,
                       },

                      @{@"itemTitle":@"Item 5",
                        @"isSelected":@NO,
                       },

                      @{@"itemTitle":@"Item 6",
                        @"isSelected":@NO,
                       },

                      @{@"itemTitle":@"Item 7",
                        @"isSelected":@NO,
                       },

                     ];

    [self.collectionView setItemPrototype:self.collectionViewItem];
    [self.collectionView setContent:self.contents];
}

@end

CustomCollectionItem.h

#import <Cocoa/Cocoa.h>

@interface CustomCollectionItem : NSCollectionViewItem

@property (nonatomic, weak) IBOutlet NSButton *button;
@property (nonatomic, weak) IBOutlet NSView *borderView;
@property (nonatomic, assign) BOOL showSelection;

- (IBAction)selectItemAction:(id)sender;

@end

CustomCollectionItem.m

#import "CustomCollectionItem.h"

@implementation CustomCollectionItem

- (void)setRepresentedObject:(id)representedObject
{
    [super setRepresentedObject:representedObject];

    if (representedObject !=nil)
    {
        [self.button setTitle:[representedObject   valueForKey:@"itemTitle"]];
        self.showSelection = [[representedObject valueForKey:@"isSelected"] boolValue];
    }
    else
    {
        [self.button setTitle:@"No Value"];
        [self setShowSelection:NO];
    }
}

#pragma mark - Button Action
- (IBAction)selectItemAction:(id)sender
{
    self.showSelection = !self.showSelection;
    if (self.showSelection)
    {
        [self.borderView.layer setBackgroundColor:[NSColor grayColor].CGColor];
    }
    else
   {
        [self.borderView.layer setBackgroundColor:[NSColor clearColor].CGColor];
    }
}

@end

Upvotes: 1

Related Questions