-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYMoreObserveableArrayController.m
57 lines (43 loc) · 1.56 KB
/
SSYMoreObserveableArrayController.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
#import "SSYMoreObserveableArrayController.h"
@interface SSYMoreObserveableArrayController ()
@property (assign) BOOL hasSelection ;
@property (assign) NSInteger countOfArrangedObjects ;
@end
@implementation SSYMoreObserveableArrayController
- (void)dealloc {
[self removeObserver:self
forKeyPath:@"selectedObjects"] ;
#if !__has_feature(objc_arc)
[super dealloc] ;
#endif
}
- (void)awakeFromNib {
// Per Discussion in documentation of -[NSObject respondsToSelector:].
// the superclass name in the following must be hard-coded.
if ([NSArrayController instancesRespondToSelector:@selector(awakeFromNib)]) {
[super awakeFromNib] ;
}
[self addObserver:self
forKeyPath:@"selectedObjects"
options:0
context:[self class]] ;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ((context == [self class]) && [keyPath isEqualToString:@"selectedObjects"]) {
NSInteger selectionCount = [[self selectedObjects] count] ;
BOOL hasSelection = (selectionCount > 0) ;
self.hasSelection = hasSelection ;
}
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context] ;
}
- (void)rearrangeObjects {
[super rearrangeObjects] ;
self.countOfArrangedObjects = ((NSArray*)self.arrangedObjects).count ;
}
@end