phranck/CNGridView — pull request #6 by slavikshen (2013-05-13 12:54:10+00:00)
Title: Add methods to support KVO change with animation

Methods for animate KVO changes

- (void)insertItemAtIndex:(NSInteger)index animated:(BOOL)animated;
- (void)insertItemsAtIndexes:(NSIndexSet*)indexes animated:(BOOL)animated;
- (void)reloadItemsAtIndexes:(NSIndexSet*)indexes animated:(BOOL)animated;
- (void)removeItemsAtIndexes:(NSIndexSet*)indexes animated:(BOOL)animated;
- (void)beginUpdates;
- (void)endUpdates;

To use those methods
First add KVO to a mutable array.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change: (NSDictionary *)change context:(void *)context {
if( object == _objController ) {
if (context == (__bridge void*)kArrayControllerProperty_Data ) {
// process change of data
[self didReceiveDataChange:change];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

- (void)didReceiveDataChange:(NSDictionary*)change {

NSKeyValueChange changeType = [[change objectForKey:NSKeyValueChangeKindKey] integerValue];
NSIndexSet* indexSet = [change objectForKey:NSKeyValueChangeIndexesKey];

CNGridView* gridView = self.gridView;

[gridView beginUpdates];

BOOL needReload = NO;
switch( changeType ) {

case NSKeyValueChangeInsertion: {
[gridView insertItemsAtIndexes:indexSet animated:YES];
}
break;

case NSKeyValueChangeRemoval: {
[gridView removeItemsAtIndexes:indexSet animated:YES];
}
break;

case NSKeyValueChangeReplacement: {
[gridView reloadItemsAtIndexes:indexSet animated:YES];
}
break;

default:
needReload = YES;
break;
}
if( needReload ) {
[gridView reloadDataAnimated:YES];
}

[gridView endUpdates];
}

You should see change animations.

Other new features
// add a header view at the beginning of the content
@property(nonatomic,strong) IBOutlet NSView* headerView;
// center align all grids
@property(nonatomic,assign) BOOL useCenterAlignment;

Enjoys :)