kolinkrewinkel/KKGridView — Issue #97: Extra Cells Added As Subviews, Then Abandoned, When Using NSFetchedResultsController
Author: AshFurrow
Created: 2012-01-16 20:02:38+00:00
State: open

I've integrated the KKGridView into my project, but I'm having a problem with extra cells being added to the grid view, then abandoned as subviews. This *isn't* apparent in the example project because the background colours of the grid view and cells are both white, so they're effectively invisible. My grid view has a transparent background, on top of an image, so it's clear in this screenshot:
![Screenshot](http://ashfurrow.com/extracells.png)
What's worse is that the extra cells are actually *on top of* the cells underneath. After setting a breakpoint in _layoutVisibleCells, I can produce this output in gdb:

(gdb) po [self visibleIndexPaths]
<__NSArrayM 0x8e3b720>(
KKIndexPath {Index: 0; Section: 0},
KKIndexPath {Index: 1; Section: 0},
KKIndexPath {Index: 2; Section: 0},
KKIndexPath {Index: 3; Section: 0},
KKIndexPath {Index: 4; Section: 0},
KKIndexPath {Index: 5; Section: 0},
KKIndexPath {Index: 6; Section: 0},
KKIndexPath {Index: 7; Section: 0},
KKIndexPath {Index: 8; Section: 0},
KKIndexPath {Index: 9; Section: 0},
KKIndexPath {Index: 10; Section: 0},
KKIndexPath {Index: 11; Section: 0},
KKIndexPath {Index: 12; Section: 0},
KKIndexPath {Index: 13; Section: 0},
KKIndexPath {Index: 14; Section: 0},
KKIndexPath {Index: 15; Section: 0},
KKIndexPath {Index: 16; Section: 0},
KKIndexPath {Index: 17; Section: 0},
KKIndexPath {Index: 18; Section: 0},
KKIndexPath {Index: 19; Section: 0},
KKIndexPath {Index: 20; Section: 0},
KKIndexPath {Index: 21; Section: 0},
KKIndexPath {Index: 22; Section: 0},
KKIndexPath {Index: 23; Section: 0}
)

But when I examine the subviews, I get the following:

(gdb) po [self subviews]
<__NSArrayM 0x8c46bc0>(
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>,
>
)

The issue appears to be using an NSFetchedResultsController:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
if (controller != self.fetchedResultsController)
return;
switch(type) {
case NSFetchedResultsChangeInsert:
[self.gridView insertItemsAtIndexPaths:[NSArray arrayWithObject:[KKIndexPath indexPathWithNSIndexPath:newIndexPath]] withAnimation:KKGridViewAnimationNone];
break;
case NSFetchedResultsChangeUpdate:
[self.gridView reloadItemsAtIndexPaths:[NSArray arrayWithObject:[KKIndexPath indexPathWithNSIndexPath:indexPath]]];
break;
}
}

If I changed the code for dealing with inserted rows to just `reloadData`, everything works fine. Could someone please advise?