Subclassing NSArrayController in order to limit size of arrangedObjects
Posted
by Simone Manganelli
on Stack Overflow
See other posts from Stack Overflow
or by Simone Manganelli
Published on 2010-05-07T01:40:36Z
Indexed on
2010/05/07
1:48 UTC
Read the original article
Hit count: 336
I'm trying to limit the number of objects in an array controller, but I still want to be able to access the full array, if necessary. A simple solution I came up with was to subclass NSArrayController, and define a new method named "limitedArrangedObjects", that returns a limited number of objects from the real set of arranged objects. (I've seen http://stackoverflow.com/questions/694493/limiting-the-number-of-objects-in-nsarraycontroller , but that doesn't address my problem.)
I want this property to be observable via bindings, so I set a dependency to arrangedObjects on it.
Problem is, when arrangedObjects is updated, limitedArrangedObjects seems not to be observing the value change in arrangedObjects. I've hooked up an NSCollectionView to limitedArrangedObjects, and zero objects are being displayed. (If I bind it to arrangedObjects instead, all the objects show up as expected.)
What's the problem?
Here's the relevant code:
@property (readonly) NSArray *limitedArrangedObjects;
- (NSArray *)limitedArrangedObjects;
{
NSArray *arrangedObjects = [super arrangedObjects];
NSUInteger upperLimit = 10000;
NSUInteger count = [arrangedObjects count];
if (count > upperLimit) count = upperLimit;
arrayToReturn = [arrangedObjects subarrayWithRange:NSMakeRange(0, count)];
return arrayToReturn;
}
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key;
{
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
if ([key isEqualToString:@"limitedArrangedObjects"]) {
NSSet *affectingKeys = [NSSet setWithObjects:@"arrangedObjects",nil];
keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKeys];
}
return keyPaths;
}
© Stack Overflow or respective owner