Skip to content

Commit

Permalink
Improve demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
kolinkrewinkel committed Apr 30, 2012
1 parent 64d1492 commit d80f3ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 62 deletions.
Expand Up @@ -25,12 +25,12 @@ - (void)viewDidLoad
{
[super viewDidLoad];

self.title = NSLocalizedString(@"Detail", @"Detail");

UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
self.gridView.backgroundView = backgroundView;

self.gridView.cellPadding = CGSizeMake(11.f, 5.f);
self.title = NSLocalizedString(@"Detail", @"Detail");

_fillerData = [[NSMutableArray alloc] init];
for (NSUInteger i = 0; i < 20; i++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
Expand All @@ -56,6 +56,11 @@ - (NSUInteger)gridView:(KKGridView *)gridView numberOfItemsInSection:(NSUInteger
return [[_fillerData objectAtIndex:section] count];
}

- (NSString *)gridView:(KKGridView *)gridView titleForHeaderInSection:(NSUInteger)section
{
return [NSString stringWithFormat:@"%u", section];
}

- (KKGridViewCell *)gridView:(KKGridView *)gridView cellForItemAtIndexPath:(KKIndexPath *)indexPath
{
KKDemoCell *cell = [KKDemoCell cellForGridView:gridView];
Expand Down
64 changes: 5 additions & 59 deletions KKGridView/KKGridView.m
Expand Up @@ -429,6 +429,7 @@ - (void)_layoutGridView
[self _layoutVisibleCells];
[self _layoutSectionViews];
[self _layoutExtremities];
[self _performRemainingUpdatesModelOnly];
_markedForDisplay = NO;
_staggerForInsertion = NO;
_needsAccessoryReload = NO;
Expand Down Expand Up @@ -467,6 +468,9 @@ - (void)_respondToBoundsChange

- (void)_performRemainingUpdatesModelOnly
{
if (_updateStack.itemsToUpdate.count == 0)
return;

NSArray *filteredArray = [_updateStack.itemsToUpdate filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"animating == NO"]];
if (filteredArray.count > 0) {
[_updateStack.itemsToUpdate removeObjectsInArray:filteredArray];
Expand Down Expand Up @@ -678,7 +682,7 @@ - (void)_layoutVisibleCells
// Updates
KKGridViewAnimation animation = KKGridViewAnimationNone;

if ([_updateStack hasUpdateForIndexPath:indexPath]) {
if ([_updateStack hasUpdateForIndexPath:indexPath] && !_batchUpdating) {
animation = [self _handleUpdateForIndexPath:indexPath visibleIndexPaths:visiblePaths];
}
KKGridViewCell *cell = [_visibleCells objectForKey:indexPath];
Expand Down Expand Up @@ -1033,64 +1037,6 @@ - (void)insertItemsAtIndexPaths:(NSArray *)indexPaths withAnimation:(KKGridViewA
_staggerForInsertion = YES;
_markedForDisplay = YES;
[self _layoutGridView];
NSArray *unaffected = [_updateStack.itemsToUpdate filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"animating == NO"]];
NSArray *visiblePaths = [self visibleIndexPaths];
if (unaffected.count > 0) {
for (KKGridViewUpdate *update in unaffected) {
// Updates
KKIndexPath *indexPath = update.indexPath;
if ([_updateStack hasUpdateForIndexPath:indexPath]) {
_markedForDisplay = YES;
_staggerForInsertion = YES;
_needsAccessoryReload = YES;

KKGridViewUpdate *update = [_updateStack updateForIndexPath:indexPath];

NSArray *newVisiblePaths = [self visibleIndexPaths];

if (update.type == KKGridViewUpdateTypeItemInsert || update.type == KKGridViewUpdateTypeItemDelete) {
[self _incrementCellsAtIndexPath:indexPath
toIndexPath:[self _lastIndexPathForSection:indexPath.section]
byAmount:update.sign];
}

NSMutableSet *replacementSet = [[NSMutableSet alloc] initWithCapacity:_selectedIndexPaths.count];

for (KKIndexPath *keyPath in _selectedIndexPaths) {
if (indexPath.section == keyPath.section)
keyPath.index += update.sign;

if (keyPath.index > 0)
[replacementSet addObject:keyPath];
};

[_selectedIndexPaths setSet:replacementSet];

[self reloadContentSize];
[_updateStack removeUpdate:update];

if (![newVisiblePaths isEqual:visiblePaths]) {
NSMutableArray *difference = [[[_visibleCells allKeys] sortedArrayUsingSelector:@selector(compare:)] mutableCopy];
[difference removeObjectsInArray:visiblePaths];
for (KKIndexPath *keyPath in difference) {
KKGridViewCell *cell = [_visibleCells objectForKey:keyPath];
cell.selected = [_selectedIndexPaths containsObject:keyPath];

[KKGridView animateIf:_staggerForInsertion delay:0.0015 options:UIViewAnimationOptionCurveEaseInOut block:^{
cell.frame = [self rectForCellAtIndexPath:indexPath];
}];
}
}
}
}
[self _cleanupCells];

[self _layoutGridView];

}
[self _performRemainingUpdatesModelOnly];

[self _commonReload];
}

- (void)deleteItemsAtIndexPaths:(NSArray *)indexPaths withAnimation:(KKGridViewAnimation)animation
Expand Down

0 comments on commit d80f3ff

Please sign in to comment.