-
Notifications
You must be signed in to change notification settings - Fork 11
/
TSExtraCollectionView.m
77 lines (56 loc) · 1.95 KB
/
TSExtraCollectionView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// TSExtraCollectionView.m
// ThisService
//
// Created by Jesper on 2012-07-09.
// Copyright 2012 waffle software. All rights reserved.
// BSD licensed - see license.txt for more information.
//
//
#import "TSExtraCollectionView.h"
#import <objc/runtime.h>
@implementation TSExtraCollectionView
-(void)awakeFromNib {
views = [[NSMutableDictionary alloc] init];
}
- (void)registerView:(NSView *)view forIdentifier:(NSString *)identifier {
[views setObject:view forKey:identifier];
}
- (void)registerWhenRemovedTarget:(id)target action:(SEL)action {
whenRemovedTarget = [target retain];
whenRemovedAction = action;
}
- (void)callWhenRemoved {
[whenRemovedTarget performSelector:whenRemovedAction withObject:self];
}
- (NSCollectionViewItem *)newItemForRepresentedObject:(id)object {
NSCollectionViewItem *newItem = [super newItemForRepresentedObject:object];
// NSLog(@"new item %@", newItem);
NSView *v = [newItem view];
TSExtraCollectionItemView *itemView = (TSExtraCollectionItemView *)v;
[itemView setItem:newItem];
NSButton *button = [itemView viewWithTag:888];
[button setTarget:itemView];
[button setAction:@selector(removeItem:)];
// NSLog(@"itemView: %@", itemView);
NSView *view = [views objectForKey:object];
[itemView addSubview:view];
return newItem;
}
@end
@implementation TSExtraCollectionItemView
@synthesize item = _item;
- (IBAction)removeItem:(id)sender {
NSMutableArray *c = [[[_item.collectionView content] mutableCopy] autorelease];
[c removeObject:_item.representedObject];
id plainCollectionView = _item.collectionView;
TSExtraCollectionView *collectionView = nil;
if ([plainCollectionView isKindOfClass:[TSExtraCollectionView class]]) {
collectionView = plainCollectionView;
}
[_item.collectionView setContent:c];
if (collectionView) {
[collectionView callWhenRemoved];
}
}
@end